Dave Burt
6/24/2006 5:54:00 AM
simonh wrote:
> Hi all, just read this on bitwisemag.com and thought I'd better check
> if it is the case:
>
> "Incidentally, the braces following gets() are optional as are the
> braces enclosing the strings after print and puts; the code would run
> just the same if you removed the braces. However, Ruby is increasingly
> moving towards the use of braces - particularly when passing
> arguments to methods - and, in some cases, the interpreter will warn
> you if you omit them."
>
> attention is drawn to the sentence beginning 'However, Ruby is...'
>
> surely this is not the case?
You are right, this is not the case, and it's not true that "Ruby is
increasingly moving toward the use of" parentheses.
The only circumstance where the interpreter will issue a warning
regarding parentheses on methods is where you have one non-parenthesised
method call within another. This is because it's not obvious to which
method trailing arguments belong. Consider the following:
foo bar x, baz
That makes sense, right? But you could read that as foo(bar(x, baz)) or
foo(bar(x), baz). Ruby chooses the former, but issues a warning. Ruby
doesn't like this kind of ambiguity because one of its aims is for code
to be as understandable by humans as by computers. Note that either of
the following forms removes the problem (both for the human reader and
for Ruby) without resorting to four sets of parentheses.
a b(c), d
a b(c, d)
Don't worry, you'll always be able to call a method using "obj.meth" syntax.
Cheers,
Dave