[FAQ] When should a method have parentheses?

Hi -talk,

An unanswered question appears on the FAQ page. Actually, a few do,
but this is oldest so I’ll tackle it first. The problem is: I have no
idea. Can someone provide an answer, and I’ll update the FAQ.

Question: When should a method have parentheses?

Comment: Sometimes Ruby 1.7.x warns that parentheses will be required
in the future.

Happy answering!

Gavin

From http://www.rubycentral.com/book/tut_methods.html

The optional parameters follow the method name. If there is no ambiguity
you can omit the parentheses around the argument list when calling a
method.[Other Ruby documentation sometimes calls these method calls
without parentheses ``commands.‘’] 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.

maybe a pointer there would be appropriate?

Gavin Sinclair wrote:

···

Hi -talk,

An unanswered question appears on the FAQ page. Actually, a few do,
but this is oldest so I’ll tackle it first. The problem is: I have no
idea. Can someone provide an answer, and I’ll update the FAQ.

Question: When should a method have parentheses?

Comment: Sometimes Ruby 1.7.x warns that parentheses will be required
in the future.

Happy answering!

Gavin

Thanks for the quick response, Dmitri. Unfortunately there is an
unwritten context to the question that you would not have gained,
having joined the list only recently.

I agree that parentheses should be used whenever there is any doubt,
and I in fact use them most of the time. However, there was a case a
while ago where ‘p (0…5)’ failed or something, and the answer was
that parentheses were required in a place where you might not expect.
I think that’s where the question came from.

Ultimately, though, I’m not sure there will be a suitable answer for
the FAQ.

Finally, please note that in this list, it is preferable to write your
reply in the context of the message, not at the top.

Cheers,
Gavin

···

On Wednesday, January 22, 2003, 3:48:40 PM, Dmitri wrote:

From http://www.rubycentral.com/book/tut_methods.html

The optional parameters follow the method name. If there is no ambiguity
you can omit the parentheses around the argument list when calling a
method.[Other Ruby documentation sometimes calls these method calls
without parentheses ``commands.‘’] 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.

maybe a pointer there would be appropriate?

Gavin Sinclair wrote:

Hi -talk,

An unanswered question appears on the FAQ page. Actually, a few do,
but this is oldest so I’ll tackle it first. The problem is: I have no
idea. Can someone provide an answer, and I’ll update the FAQ.

Question: When should a method have parentheses?

Comment: Sometimes Ruby 1.7.x warns that parentheses will be required
in the future.

Happy answering!

Gavin

Gavin Sinclair wrote:

Thanks for the quick response, Dmitri. Unfortunately there is an
unwritten context to the question that you would not have gained,
having joined the list only recently.

ahhh, one of them… ok, I’ll sit back then (o:

[snip]

Finally, please note that in this list, it is preferable to write your
reply in the context of the message, not at the top.

:smiley: I cant resist… yeah, I was lazy… but erm… “do as I say not
as I do”? hehe…

cheers
dim