From: Rob Biedenharn Date: 2008-02-07T13:04:06+09:00 Subject: Re: idiom I've not seen before 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