From: Rick DeNatale Date: 2008-01-09T00:39:26+09:00 Subject: Re: proc to block On Jan 8, 2008 9:05 AM, Noah Easterly wrote: > On Jan 8, 8:24 am, Rick DeNatale wrote: > > On Jan 7, 2008 11:35 PM, Gary Wright wrote: > > > Just prefix the object you want to be passed as a block with '&' in > > > the argument list. > > > > > p = proc { |x| x > 10 } > > > > > [1,5,10,20, 30].select &p > > > > Except that this isn't part of the standard ruby library for 1.8. It > > relies on having a Symbol#to_proc method > > > Wait... what? > > No it doesn't. > > [ 1, 5, 10, 20, 30 ].map &:to_a > > uses Symbol#to_proc > > p = proc { |x| x > 10 } > [1,5,10,20, 30].select &p > > is standard ruby syntax, even from 1.6: > > "If the last argument to a method is preceded by an ampersand, Ruby > assumes that it is a Proc object. It removes it from the parameter > list, converts the Proc object into a block, and associates it with > the method." This isn't what I said. Gary suggested using a symbol as a proc, which requires that Symbol implement the to_proc method, which is NOT part of standard Ruby prior to 1.9. $ qri to_proc ------------------------------------------------------ Multiple choices: Method#to_proc, Proc#to_proc, Test::Unit::Util::ProcWrapper#to_proc Now as I pointed out extending Symbol to implement to_proc is quite common, and many Rails programmers tend to think of the stuff in ActiveSupport as being standard Ruby, but it isn't. It's likely that Rails will change ActiveSupport to only add Symbol#to_proc if using Ruby < 1.9 since 1.9 includes it, but that doesn't seem to have happened yet. http://dev.rubyonrails.org/ticket/8818 -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/