From: Trans Date: 2005-11-02T09:57:07+09:00 Subject: Re: Anonymous methods, blocks etc. (Cont. 'default block params') Hi-- gwtmp01@mac.com wrote: > Something tells me that it will be impossible to have an identical > syntax for all of: > > 1) local variable > 2) 0-argument method invocation > 3) formal parameters > 4) 0-argument Proc#call invocation What do you mean by #3? Other than that why so you see this as problem? If a method and a local var can be the same why not annoynous method with local var name? The trick is simplly like that of dealing with blocks --though I realize now some other symbolization will be needed to differentiate block from passing first-class lambda object. I'm gogin to use Matz notation here for lack of anything else (interestingly it actually makes some sense as "dereference") def pass_it_on( x ) x # calls it ->x # returns it back end inc = ->(x){ x+1 } pass_it_on( ->inc ) So to pass a lambda around you just prefix it. Interesing thing you can do is easy lazy evaluation: def diff( a, b ) a - b end sec = ->{ sleep 1 ; Time.now.sec } diff( ->sec, ->sec ) #=> 1, or there abouts ;) This can be applied to methods to, eg. def mymeth(a,b,c) # ... end def mymeth_size ->mymeth.arity end > It is pretty easy to package up define_method and have a very concise > way to define methods on demand. This lets you associate a block with a > name (a method name) at will. The only "gotcha" is that you are working > within the namespace of the object's methods and not the namespace of > the the method itself (as with local variables). > > class A > def fn(symbol, &block) > (class < end > def method_missing(symbol, *args, &block) > fn(symbol, &block) > end > > def m1 > m2 {|n| 2*n} > fn(:m3){|n| 3*n } > > puts m2(4) # -> 8 > puts m3(4) # -> 12 > end > end > > A.new.m1 Sure, but this is very weak by comparision. Your using method_missing for one, and as you say your defining methods into the singleton space, not anonymously. Ruby has all sorts of "tricks", but this isn't a suitable general solution. T.