In Ruby any operator has precedence over method calls, e.g. operators are evaluated first.
However the example Math.sqrt 2 + 2
is a good illustration on how hard to read and unintuitive it can be to leave out parentheses. One might expect Math.sqrt(2) + 2
to be evaluated here.
When you come across a line like this, you might think: What did the coder intend? Is this a bug?
It is always a good idea to use parentheses whenever you can, to make clear what you want,
especially when there is a method call or multiple operators - it's just good style, and the lowest risk approach (e.g. don't make assumptions, but make yourself clear by using parentheses).
It never hurts to add extra parentheses in expressions, but leaving them out can hurt quite a bit.
Here's a nice example which I recently came across:
def foo(arg)
raise "error"
end
x = foo 4 rescue 7
x
=> nil # oops!
x = foo(4) rescue 7
x
=> 7
I hope this nicely illustrates why it's important to use parentheses
Also:
Check here, under "Calling a Method" : http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
Quote:
[...] If there is no
ambiguity you can omit the parentheses around the argument list when
calling a method.[...] However, except in the simplest cases we don't
recommend this---there are some subtle problems that can trip you
up.[In particular, you must use parentheses on a method call that is
itself a parameter to another method call (unless it is the last
parameter).] Our rule is simple: if there's any doubt, use
parentheses.
See also: http://phrogz.net/programmingruby/language.html