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

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

    Although we have great answers already, looking through a perspective of a beginner I'd like to add the additional information:

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

    This means, that you are passing another method as parameter to the map function. (In reality you're passing a symbol that gets converted into a proc. But this isn't that important in this particular case).

    What is important is that you have a method named name that will be used by the map method as an argument instead of the traditional block style.

提交回复
热议问题