[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

simonh

6/23/2006 6:15:00 PM

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?

3 Answers

Gene Tani

6/23/2006 6:36:00 PM

0


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?

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+R...

Huw Collingbourne

6/23/2006 7:22:00 PM

0

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.
================================



Dave Burt

6/24/2006 5:54:00 AM

0

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