Possible Duplicate:
What does map(&:name) mean in Ruby?
Ruby/Ruby on Rails ampersand colon shortcut
For example,
contacts.sort_by(&:first_name).
I understand what this does, but I dont understand the &: notations, what does that mean, is it a symbol(:) with a block (&)? Where can I read more about it?
When
&used before Proc object in method invocation, it treats the Proc as if it was an ordinary block following the invocation.When
&used before other type of object (symbol:first_namein your case) in method invocation, it tries to call to_proc on this object and if it does not have to_proc method you will getTypeError.Generally
&:first_nameis the same as&:first_name.to_proc.:first_name.to_procwill return Proc that looks like this:this Proc invokes method specified by original symbol on the object passes as the first parameter and pass all the rest parameters + block as this method arguments.
One more example: