Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does map(&:name) mean in Ruby?

I found this code in a RailsCast:

def tag_names   @tag_names || tags.map(&:name).join(' ') end 

What does the (&:name) in map(&:name) mean?

like image 288
collimarco Avatar asked Aug 01 '09 17:08

collimarco


People also ask

What does the acronym MAP stand for?

MAP: An acronym for "minor-attracted person".

What does MAP twitter mean?

MAP is an initialism, self-created by the proud individuals it references, as “Minor Attracted Persons.”

What does MAP stand for in business?

MAP stands for “minimum advertised price,” and a MAP policy is a legal document brands use to define the lowest possible price a product can legally be advertised for.


2 Answers

It's shorthand for tags.map(&:name.to_proc).join(' ')

If foo is an object with a to_proc method, then you can pass it to a method as &foo, which will call foo.to_proc and use that as the method's block.

The Symbol#to_proc method was originally added by ActiveSupport but has been integrated into Ruby 1.8.7. This is its implementation:

class Symbol   def to_proc     Proc.new do |obj, *args|       obj.send self, *args     end   end end 
like image 114
Josh Lee Avatar answered Sep 28 '22 12:09

Josh Lee


Another cool shorthand, unknown to many, is

array.each(&method(:foo)) 

which is a shorthand for

array.each { |element| foo(element) } 

By calling method(:foo) we took a Method object from self that represents its foo method, and used the & to signify that it has a to_proc method that converts it into a Proc.

This is very useful when you want to do things point-free style. An example is to check if there is any string in an array that is equal to the string "foo". There is the conventional way:

["bar", "baz", "foo"].any? { |str| str == "foo" } 

And there is the point-free way:

["bar", "baz", "foo"].any?(&"foo".method(:==)) 

The preferred way should be the most readable one.

like image 31
Gerry Avatar answered Sep 28 '22 11:09

Gerry