[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: syntax sugar: treating an object like a method

Eric Mahurin

5/19/2005 2:33:00 PM

> > It'll probably work as general mapping to #call so it would
> > work with
> > everything that has #call.
> >
> > Cheers
> >
> > robert
>
> Thanks Robert. If that is the case, it sounds like I'll get
> the main thing I wanted - ()/null operator.
>
> Anybody know if lambdas will also support blocks anytime
> soon?
> It doesn't seem to make sense why I can't do this:
>
> f = lambda {|*args,&block|puts(*args);block[]}
> f[1,2,3] { puts("hi") }
>
> or in the 1.9 syntax:
>
> f = {|*args,&block|puts(*args);block[]}
> f(1,2,3) { puts("hi") }
>
> I just grabbed 1.9 and none of this new syntax seems to work.

whoops. I was still running 1.8.2.

It looks like you can put &block as a lambda argument now. So
the above 1.9 example works.

Unfortunately, when "calling" an object, you have to do it from
a local variable. Also, you have to have the parens in there
even if you have a block. These don't work:

{|*args,&block|puts(*args);block()}(1,2,3) { puts("hi") }
f { puts("hi") } # f() { puts("hi") } works

I'm not sure why f can't be an arbitrary expression instead. I
also tried this:

class Class;alias call new;end

This doesn't work:

Range(0,7)

and this does:

range = Range
range(0,7) -> 0..7

And one more thing. If someone has both a local variable f and
a method f, we have different behavior in 1.8.2 vs. 1.9:

f = lambda {|*args|puts("lambda",*args)}
def f(*args);puts("method",*args);end
f(1,2,3)

1.8.2 calls the method and 1.9 calls the lambda. This seems
bad. So it looks like the priority between local variables and
methods has been reversed - 1.8.2 prefers methods and 1.9
prefers local variables. You can no longer use "()" to
distinguish the two. Maybe is does make sense because they are
in the same namespace and local usually overrides, but people
need to be aware.




__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/...


1 Answer

Christian Neukirchen

5/19/2005 3:36:00 PM

0

Eric Mahurin <eric_mahurin@yahoo.com> writes:

> And one more thing. If someone has both a local variable f and
> a method f, we have different behavior in 1.8.2 vs. 1.9:
>
> f = lambda {|*args|puts("lambda",*args)}
> def f(*args);puts("method",*args);end
> f(1,2,3)
>
> 1.8.2 calls the method and 1.9 calls the lambda. This seems
> bad. So it looks like the priority between local variables and
> methods has been reversed - 1.8.2 prefers methods and 1.9
> prefers local variables. You can no longer use "()" to
> distinguish the two. Maybe is does make sense because they are
> in the same namespace and local usually overrides, but people
> need to be aware.

Wasn't that just what you wanted?
Remember that Ruby 1.8 cannot call lambdas directly.

If one decides to call #call magically (which I'm quite opposed to),
local variables should have preferrence, I think.

Finally, () only defines evaluation order/parameters, there is no way
(and there shouldn't be, IMO) a method will behave differently when
called with or without parentheses.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...