Possible Duplicate:
What does map(&:name) mean in Ruby?
I came across a code snippet which had the following
a.each_slice(2).map(&:reverse)
I do not know the functionality of &: operator. How does that work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There isn’t a
&:operator in Ruby. What you are seeing is the&operator applied to a:symbol.In a method argument list, the
&operator takes its operand, converts it to aProcobject if it isn’t already (by callingto_procon it) and passes it to the method as if a block had been used.So the question now becomes “What does
Symbol#to_procdo?”, and that’s easy to see in the Rails documentation: