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

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

    tags.map(&:name)
    

    is The same as

    tags.map{|tag| tag.name}
    

    &:name just uses the symbol as the method name to be called.

提交回复
热议问题