[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Parenthesis in method call

minkoo.seo@gmail.com

4/25/2006 8:03:00 AM

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

8 Answers

Ross Bamford

4/25/2006 8:36:00 AM

0

On Tue, 25 Apr 2006 09:03:02 +0100, Minkoo Seo <minkoo.seo@gmail.com>
wrote:

> 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')
>

This thread might be of interest:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

--
Ross Bamford - rosco@roscopeco.remove.co.uk

minkoo.seo@gmail.com

4/25/2006 10:03:00 AM

0

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

Ross Bamford

4/25/2006 10:22:00 AM

0

On Tue, 25 Apr 2006 11:03:04 +0100, Minkoo Seo <minkoo.seo@gmail.com>
wrote:

> 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)

--
Ross Bamford - rosco@roscopeco.remove.co.uk

minkoo.seo@gmail.com

4/25/2006 10:28:00 AM

0

Ross Bamford wrote:
>
> 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

Daniel Schierbeck

4/25/2006 8:39:00 PM

0

Minkoo Seo wrote:
> 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

Daniel Schierbeck

4/25/2006 8:44:00 PM

0

Minkoo Seo wrote:
> Ross Bamford wrote:
>> 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.

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

minkoo.seo@gmail.com

4/26/2006 10:22:00 AM

0


Daniel Schierbeck wrote:
> Minkoo Seo wrote:
> > 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.
>

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

minkoo.seo@gmail.com

4/26/2006 10:23:00 AM

0


Minkoo Seo wrote:
> Daniel Schierbeck wrote:
> > Minkoo Seo wrote:
> > > 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.
> >
>
> 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.