From: Deepak Gole Date: 2008-02-08T20:33:35+09:00 Subject: Re: idiom I've not seen before ------=_Part_29724_17736095.1202470422152 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello Rob We can send arguments, Following is the way, But it throws an error. def to_proc Proc.new { |*args| args.shift.__send__(self, *args) } end arr.collect(&[:concat,"Rocking"]) * *sample.rb:41:in `__send__': [:concat, "dee"] is not a symbol (TypeError) from sample.rb:41:in `to_proc' from sample.rb:51:in `collect' from sample.rb:51 But if we change the implementation of to_proc then it works. def to_proc lambda {|target| target.send *self} end arr.collect(&[:concat,"Rocking"]) o/p => ["rubyRocking", "railsRocking", "rjaxRocking"] I don't know how. On Feb 6, 2008 8:04 PM, Rob Biedenharn wrote: > On Feb 6, 2008, at 10:55 PM, Rob Saul wrote: > > I came across this : > > elements.sort_by(&:position).each > > in something I've taken over. I thought sort_by > > took a block. Obviously I'm missing something. > > Can anyone shed some light on this trick? > > > > ~Rob > > > You've found Symbol#to_proc > > That line is equivalent to: > > elements.sort_by {|e| e.send(:position) }.each > > The "something" is a Rails project, isn't it? > > == vendor/rails/activesupport/lib/active_support/core_ext/symbol.rb == > class Symbol > # Turns the symbol into a simple proc, which is especially useful > for enumerations. Examples: > # > # # The same as people.collect { |p| p.name } > # people.collect(&:name) > # > # # The same as people.select { |p| p.manager? }.collect { |p| > p.salary } > # people.select(&:manager?).collect(&:salary) > def to_proc > Proc.new { |*args| args.shift.__send__(self, *args) } > end > end > > > I don't know how you can send additional arguments however. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob@AgileConsultingLLC.com > > > > ------=_Part_29724_17736095.1202470422152--