is ruby going to require parentheses for puts, print etc?

S

simonh

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?
 
G

Gene Tani

simonh said:
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?

Can you link to the article? I think they're talking about certain
very special cases like the lambda's without #call

http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l9
 
H

Huw Collingbourne

As the author of that article, perhaps I can explain. There are many cases
in Ruby when actual or apparent ambiguity may result from the omission of
parentheses. While this is probably rarely apparent with puts and gets it
does nevertheless occur. For example, run this code:

def x( y )
return y.reverse
end

y = "hello"

puts x y

Ruby will display the following warning:

warning: parenthesize argument(s) for future version

The line to which it refers is this:

puts x y

While there may be little potential for actual ambiguity in the above code
snippet, consider this:

def x( y )
return y.reverse
end

def y
return "Cheerio!"
end

x = "goodbye"
y = "hello"

puts x y

What does puts actually display? In its present form, it is not obvious. It
could be "olleh"; it could be "!oireehC". In fact, it is the former, but a
few brackets would avoid any confusion:

puts( x y() ) #=> prints "!oireehC"
puts( x( y ) ) #=> prints "olleh"

While brackets are optional and many people prefer to omit them whenever
possible, there are circumstances when a couple of brackets can avoid a
great deal of confusion - not to mention the "parenthesize argument(s) for
future version" warning message ;-)

best wishes
Huw Collingbourne
================================
Bitwise Magazine
www.bitwisemag.com
Dark Neon Ltd.
================================
 
D

Dave Burt

simonh said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,207
Messages
2,571,078
Members
47,682
Latest member
TrudiConna

Latest Threads

Top