Ruby operator precedence table
Ruby 2.1.0, 2.0, 1.9, 1.8
An operator is a token that represents an operation (such as addition or comparison) to be performed on one or more operands. The operands are expressions, and operators allow us to combine these operand expressions into larger expressions. (Ref)
N = arity = The number of operands the operator operates on. (Ref)
A = associativity = The order of evaluation when the same operator (or operators with the same precedence) appear sequentially in an expression. The value L
means that expressions are evaluated from left to right. The value R
means that expressions are evaluated from right to left. And the value N
means that the operator is nonassociative and cannot be used multiple times in an expression without parentheses to specify the evaluation order. (Ref)
M = definability = Ruby implements a number of its operators as methods, allowing classes to define new meanings for those operators. Column M
of specifies which operators are methods. Operators marked with a Y
are implemented with methods and may be redefined, and operators marked with an N
may not. (Ref)
The following table is ordered according to descending precedence (highest precedence at the top).
N A M Operator(s) Description
- - - ----------- -----------
1 R Y ! ~ + boolean NOT, bitwise complement, unary plus
(unary plus may be redefined from Ruby 1.9 with +@)
2 R Y ** exponentiation
1 R Y - unary minus (redefine with -@)
2 L Y * / % multiplication, division, modulo (remainder)
2 L Y + - addition (or concatenation), subtraction
2 L Y << >> bitwise shift-left (or append), bitwise shift-right
2 L Y & bitwise AND
2 L Y | ^ bitwise OR, bitwise XOR (exclusive OR)
2 L Y < <= >= > ordering
2 N Y == === != =~ !~ <=> equality, pattern matching, comparison
(!= and !~ may not be redefined prior to Ruby 1.9)
2 L N && boolean AND
2 L N || boolean OR
2 N N .. ... range creation (inclusive and exclusive)
and boolean flip-flops
3 R N ? : ternary if-then-else (conditional)
2 L N rescue exception-handling modifier
2 R N = assignment
2 R N **= *= /= %= += -= assignment
2 R N <<= >>= assignment
2 R N &&= &= ||= |= ^= assignment
1 N N defined? test variable definition and type
1 R N not boolean NOT (low precedence)
2 L N and or boolean AND, boolean OR (low precedence)
2 N N if unless while until conditional and loop modifiers
Operator precedence for And/&& in Ruby
I don't quite understand the question you are asking. I mean, you have already given the answer yourself, before even asking the question: &&
binds tighter than =
while and
binds less tightly than =
.
So, in the first case, the expression is evaluated as follows:
( a=f(2) ) and ( b=f(4) )
( a= 2 ) and ( b=f(4) )
2 and ( b=f(4) ) # a=2
2 and ( b= 4 ) # a=2
2 and 4 # a=2; b=4
4 # a=2; b=4
In the second case, the evaluation is as follows:
a = ( f(2) && ( b=f(4) ) )
a = ( 2 && ( b=f(4) ) )
a = ( 2 && ( b= 4 ) )
a = ( 2 && 4 ) # b=4
a = 4 # b=4
4 # b=4; a=4
Operator precedence for And and Or
It's simple - since &&
has higher precedence than ||
, it will be interpreted as
(app && ip) || device
I'd recommend to always add the parentheses for readability purposes, though.
See e.g. Rosettacode page on operator precedence for Ruby
Ruby operator precedence
If x
is integer, then x**2.to_s
is x.**(2.to_s)
. Because Fixnum#**
method exist. In your case **
is a method, not an operator.
In this case, no operator precedence and associativity comes into existence.
In the expression - x.**(2.to_s)
x
isFixnum
object.**
is a method called onx
.2.to_s
is simply an expression, which will be evaluated first, and passed as an argument to the method**
.
My answer is as per your example. But there are several edge cases in Ruby. Just to get the taste of those, give some time to read this thread why does a + f b
not parse?.
What's the precedence of method calls with and without parentheses?
Prelude
This aims to test all possible scenarios.
Note that when saying "operator X
has higher precedence than method invocation" what is meant is in arguments. Aka:
invocation foo X bar
as opposed to (call on object)
X invocation
As far as the second case is concerned, method calls always have higher precedence.
Short answer
It doesn't fit:
- It causes
SyntaxError
in some cases - It has higher precedence than
rescue
, but lower than assignment
Summary
not
can't be used after method invocation regardless of brackets- Using brackets (
()
) with method invocations sometimes causes aSyntaxError
. These cases are:and
,or
,if
,unless
,until
,while
andrescue
- In cases when brackets don't cause an error, they don't change the precedence in any way
- All operators, except for
and
,or
, postfixif
,unless
,until
,while
,rescue
have higher precedence than method invocation
Lets try it:
class Noone < BasicObject
undef_method :!
def initialize(order)
@order = order
end
def method_missing(name, *args)
@order << name
self
end
end
First unary:
# + and - will become binary
unary_operators = %i(! ~ not defined?)
puts 'No brackets'
unary_operators.each do |operator|
puts operator
order = []
foo = Noone.new order
bar = Noone.new order
begin
eval("foo.meta #{operator} bar")
rescue SyntaxError => e
puts e
end
p order
puts '-----------'
end
puts 'Brackets'
unary_operators.each do |operator|
puts operator
order = []
foo = Noone.new order
bar = Noone.new order
begin
eval("foo.meta(#{operator} bar)")
rescue SyntaxError => e
puts e
end
p order
puts '-----------'
end
Points taken:
not
after a method invocation is aSyntaxError
- all unary operators have higher precedence than method invocation regardless of brackets
Now binary:
binary_operators = %i(
**
* / %
+ -
<< >>
&
| ^
> >= < <=
<=> == === =~
.. ...
or and
)
puts 'No brackets'
binary_operators.each do |operator|
order = []
foo = Noone.new order
bar = Noone.new order
baz = Noone.new order
begin
eval("foo.meta bar #{operator} baz")
rescue SyntaxError => e
puts e
end
p order
end
puts 'Brackets'
binary_operators.each do |operator|
order = []
foo = Noone.new order
bar = Noone.new order
baz = Noone.new order
begin
eval("foo.meta( bar #{operator} baz)")
rescue SyntaxError => e
puts e
end
p order
end
Points taken:
- brackets around method invocation with
and
oror
is aSyntaxError
- we have to test
and
andor
further without brackets ..
and...
call<=>
. We have to test this further- we couldn't test a few other binary operators this way, namely
&&
,||
,==
,!=
, modifierrescue
,if
,unless
,until
,while
- other than the above mentioned, operators have higher precedence, regardless of brackets
def yes
puts 'yes'
true
end
def no
puts 'no'
false
end
def anything(arg)
puts 'Anything'
arg
end
anything yes and no
anything no or yes
anything yes && no
anything no || yes
anything(yes && no)
anything(no || yes)
anything yes == no
anything(yes == no)
anything yes != no
anything(yes != no)
Points taken:
and
andor
have lower precedence without brackets&&
,||
,==
and!=
have higher precedence regardless of brackets
def five(*args)
p args
5
end
five 2..7
five(2..7)
five 2...7
five(2...7)
Points taken:
..
and...
have higher precedence regardless of brackets
anything yes if no
anything(yes if no)
anything no unless yes
anything(no unless yes)
anything no until yes
anything(no until yes)
anything yes while no
anything(yes while no)
Points taken:
- brackets with
if
,unless
,until
,while
cause aSyntaxError
- all of the above have lower precedence than method invocation without brackets
def error
puts 'Error'
raise
end
anything error rescue yes
anything(error rescue yes)
Points taken:
- brackets around
rescue
cause aSyntaxError
rescue
has lower precedence if no brackets are present
Ternary:
anything yes ? no : 42
anything(yes ? no : 42)
Points taken:
- ternary has higher precedence regardless of brackets
Assignment (left for last as it changes yes
and no
):
anything yes = no
anything(no = five(42))
Points taken:
- Assignment has higher precedence than invocation
Note that +=
and the like are just shortcuts for +
and =
so they exhibit the same behaviour.
Logic OR / AND term evaluation order in Ruby
Ruby community commonly used is short-circuit
evaluation which is &&
stands for AND
, ||
stands for OR
It means in a conditional statement with two conditions the second condition is evaluated only when the first condition is not enough to determine the value of expression
so let's take an example for &&
, ||
def a
puts 'a method'
false
end
def b
'b method'
true
end
a && b
# 'a method'
# => false
b is never evaluated
def a
puts 'a method'
true
end
def b
'b method'
false
end
a || b
# 'a method'
# => true
b is also never evaluated
there is another construction and
, or
(lowercased)
The and and or keywords serve the same purpose in Ruby. Properly understood, and and or are control flow operators, not boolean operators.
You use them as more like control-flow operators if
, unless
. This operations have very low operator precedence.
Further reading:
and or and short-curcuit difference
short-curcuit
Related Topics
How to Avoid Nomethoderror For Missing Elements in Nested Hashes, Without Repeated Nil Checks
How to Sort an Array in Descending Order in Ruby
Code Block Passed to Each Works With Brackets But Not With 'Do'-'End' (Ruby)
Bundle Install Fails With Ssl Certificate Verification Error
Is 'Eval' Supposed to Be Nasty
Pg::Connectionbad - Could Not Connect to Server: Connection Refused
What Is the Easiest Way to Duplicate an Activerecord Record
Difference or Value of These Block Coding Styles in Ruby
How to Make --No-Ri --No-Rdoc the Default For Gem Install
In Ruby How to Overload the Initialize Constructor
Rails Activerecord: Find All Users Except Current User
Accessing Elements of Nested Hashes in Ruby
Difference Between Stdin and $Stdin in Ruby
How to Get the Match Data For All Occurrences of a Ruby Regular Expression in a String