From: Florian Gilcher Date: 2009-03-22T17:01:25+09:00 Subject: Re: Passing a named function instead of a code block? On Mar 22, 2009, at 4:20 AM, 7stud -- wrote: > James Coglan wrote: > > Thanks for the explanation. You said that (&) casts between block and > Procs. But (&) also appears to be casting a Method to a block in this > line: > > puts c.collect(&method(:fib)) > > (&) can also be used to cast in the reverse direction, e.g. from a > block > to a Proc: > > def test(&aProc) > aProc.call(10) > end > > test {|x| puts x} > > --output:-- > 10 > > > Can (&) cast a block to a Method? There are no casts in Ruby. & in a Method call calls #to_proc on an Object and expects it to return a proc. Thats a conversion that has to be done explicitly by the programmer. & in a Method definition indicates that the argument has to be a Proc or something that responds to #to_proc (or a passed Block) which then gets enforced. Blocks are Procs (AFAIK, internally, Methods are basically procs as well). The only magic is "yield" together with a block, which to my knowledge doesn't require the construction of a Proc Object that has to be visible to the programmer. But thats up to the implementation. Regards, Florian -- Florian Gilcher smtp: flo@andersground.net jabber: Skade@jabber.ccc.de gpg: 533148E2