From: Noah Easterly Date: 2008-01-08T23:05:00+09:00 Subject: Re: proc to block On Jan 8, 8:24 am, Rick DeNatale wrote: > On Jan 7, 2008 11:35 PM, Gary Wright wrote: > > > > > On Jan 7, 2008, at 11:06 PM, L A wrote: > > > > Hello all, > > > > Is there a way to turn a Proc into a block for the purposes of > > > methods that yield to blocks? It's not a necessity as I can always > > > wrap the Proc in a block, I'm just curious. > > > 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." (see http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html#UD)