From: "David A. Black" Date: 2009-07-05T21:41:03+09:00 Subject: Re: to_proc and Proc/block conversion with & On Sun, 5 Jul 2009, David A. Black wrote: > Hi -- > > On Sun, 5 Jul 2009, Robert Dober wrote: > >> On 7/5/09, David A. Black wrote: >>> Yes; the & means: take what's to the right of it (which can be any >>> expression), evaluate it, call to_proc on the resulting object, and >>> use the return value of to_proc to play the code block role. >>> >>> In the case of a lambda, to_proc just returns the receiver. So you can >>> do: >>> >>> array.map &lambda {|x| x * 10 } >> It might be interesting to note that in Ruby1.9 the to_proc is not >> called on Proc objects, I wonder what happened in 1.8 > > I guess it's an optimization. So take my explanation as behavioral > rather than implementation-faithful. Also, it seems to be the same in 1.8. So I guess a refinement of the description would be: given &expr, if expr is a Proc object, use it as the block; if not, call to_proc on it. I don't know whether that's a language spec or just an implementation detail. I vaguely hope the latter, though I suppose that something like this: func = lambda {|x| x * 10 } def func.to_proc # some other proc end would be extremely rare.... David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN)