Parenthesis in method call

M

Minkoo Seo

Hi group.

I got a question on the issue of inserting parenthesis before/after
arguments. For example,

irb(main):018:0> 1.+2
=> 3
irb(main):019:0> puts 1.+2
(irb):19: warning: parenthesize argument(s) for future version
3
=> nil
irb(main):020:0> quit

1.+2 calls + method with argument 2. And it runs well. On the contrary,
puts 1.+2 calls the method + of 1, and then calls puts with arguments
3. The latter case raises a warning while the former does not.

Another example:

class Foo
def initialize
@bar1, @bar2 = 0
end

attr_accessor :bar1, :bar2
end

attr_accessor is a method call, but I don't need parenthesis here.

So my question is,

(1) When do I have to use parenthesis for arguments?
(2) Why the Ruby interpreter wants me to insert parenthesis even if it
works very well without it? (I got the right answer, 3, when I run
'puts 1.+2')

Sincerely,
Minkoo Seo
 
M

Minkoo Seo

Then, I guess parenthesis is needed only if an expression or a
funcation call with arguments is used as an argument to the other
function like:

Array.new 3, 1 (Okay. Two arguments for Array.new)
p Array.new 3, 1 (Warning.)

1.+2 (Okay. One argument for 1.+)
p 1.+2 (Warning.)

The warning is raised because "p foo a, b" can be either "p(foo(a,b))"
or "p(foo(a), b)." However I also have a counterexample to my theory:

1+2 (Okay, and it is an obvious method call "1.+(2)")
p 1+2 (1+2 is passed as an argument to p, but I got no warning. Why?)
p 1.+2 (Warning. This make sense.)

Any comments?

Sincerely,
Minkoo Seo
 
R

Ross Bamford

Then, I guess parenthesis is needed only if an expression or a
funcation call with arguments is used as an argument to the other
function like:

Array.new 3, 1 (Okay. Two arguments for Array.new)
p Array.new 3, 1 (Warning.)

1.+2 (Okay. One argument for 1.+)
p 1.+2 (Warning.)

The warning is raised because "p foo a, b" can be either "p(foo(a,b))"
or "p(foo(a), b)." However I also have a counterexample to my theory:

1+2 (Okay, and it is an obvious method call "1.+(2)")
p 1+2 (1+2 is passed as an argument to p, but I got no warning. Why?)
p 1.+2 (Warning. This make sense.)

Any comments?

I think p 1+2 doesn't produce a warning because these expressions are
treated specially by the parser, so the implied knowledge of the .+ .- etc
methods means there's no ambigutity at parse time. It's effectively always
parsed as:

p 1.+(2)
 
M

Minkoo Seo

Ross said:
I think p 1+2 doesn't produce a warning because these expressions are
treated specially by the parser, so the implied knowledge of the .+ .- etc
methods means there's no ambigutity at parse time. It's effectively always
parsed as:

p 1.+(2)

Agreed. I guess that's what it is, too.

Sincerely,
Minkoo Seo
 
D

Daniel Schierbeck

Minkoo said:
irb(main):018:0> 1.+2
=> 3
irb(main):019:0> puts 1.+2
(irb):19: warning: parenthesize argument(s) for future version

Always put a `0' after the point:

puts 1.0 + 2

This does not throw a warning.


Cheers,
Daniel
 
D

Daniel Schierbeck

Minkoo said:
Agreed. I guess that's what it is, too.

Yes, but it has the same ambiguity as calling `foo bar baz'. What if
`#+' had more than one argument? I.e. `foo bar baz, bur' - should it be
interpreted as `foo(bar(baz, bur))' or `foo(bar(baz), bur)'? It's not a
problem when only one argument is passed, but for the sake of
consistency, the use of nested, unparanthesized method calls is deprecated.


Cheers,
Daniel
 
M

Minkoo Seo

Daniel said:
Always put a `0' after the point:

puts 1.0 + 2

This does not throw a warning.

No no.. You got this wrong.

1.+2 is actually represents foo.bar 2 where foo is 1 and bar is +, but
1.0 + 2 is foo bar 2 where foo is 1.0 and bar is +.

Minkoo Seo
 
M

Minkoo Seo

Minkoo said:
No no.. You got this wrong.

1.+2 is actually represents foo.bar 2 where foo is 1 and bar is +, but
1.0 + 2 is foo bar 2 where foo is 1.0 and bar is +.

Minkoo Seo

Sorry. I messed up. Forget about the previous posting.

1.+2 means calling + explicitly using '.' like foo.bar which returns an
integer, while 1.0 + 2 returns float.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top