From: Robert Dober Date: 2009-07-05T20:56:33+09:00 Subject: Re: to_proc and Proc/block conversion with & 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 536/37 > ruby -rprofile -e '[1].map &Proc::new{|x| x+1}' % cumulative self self total time seconds seconds calls ms/call ms/call name 0.00 0.00 0.00 1 0.00 0.00 BasicObject#initialize 0.00 0.00 0.00 1 0.00 0.00 Proc#new 0.00 0.00 0.00 1 0.00 0.00 Fixnum#+ 0.00 0.00 0.00 1 0.00 0.00 Array#map 0.00 0.01 0.00 1 0.00 10.00 #toplevel 537/38 > ruby -rprofile -e '[1].map &lambda{|x| x+1}' % cumulative self self total time seconds seconds calls ms/call ms/call name 0.00 0.00 0.00 1 0.00 0.00 Kernel.lambda 0.00 0.00 0.00 1 0.00 0.00 Fixnum#+ 0.00 0.00 0.00 1 0.00 0.00 Array#map 0.00 0.01 0.00 1 0.00 10.00 #toplevel ruby -rprofile -e '[1].map(&:succ)' % cumulative self self total time seconds seconds calls ms/call ms/call name 0.00 0.00 0.00 1 0.00 0.00 Kernel.proc >0.00 0.00 0.00 1 0.00 0.00 Symbol#to_proc< 0.00 0.00 0.00 1 0.00 0.00 Fixnum#succ 0.00 0.00 0.00 1 0.00 0.00 Array#map 0.00 0.01 0.00 1 0.00 10.00 #toplevel Cheers Robert -- Toutes les grandes personnes ont d’abord été des enfants, mais peu d’entre elles s’en souviennent. All adults have been children first, but not many remember. [Antoine de Saint-Exupéry]