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

后端 未结 16 2517
时光取名叫无心
时光取名叫无心 2020-11-21 05:34

I found this code in a RailsCast:

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

What does the (&:name)

16条回答
  •  余生分开走
    2020-11-21 06:04

    Here :name is the symbol which point to the method name of tag object. When we pass &:name to map, it will treat name as a proc object. For short, tags.map(&:name) acts as:

    tags.map do |tag|
      tag.name
    end
    

提交回复
热议问题