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

后端 未结 16 2539
时光取名叫无心
时光取名叫无心 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 05:57

    It's equivalent to

    def tag_names
      @tag_names || tags.map { |tag| tag.name }.join(' ')
    end
    

提交回复
热议问题