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

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

    It is same as below:

    def tag_names
      if @tag_names
        @tag_names
      else
        tags.map{ |t| t.name }.join(' ')
    end
    

提交回复
热议问题