我在RailsCast中找到了这段代码:

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

map(&:name)中的(&:name)是什么意思?


当前回答

它的意思是

array.each(&:to_sym.to_proc)

其他回答

它等价于

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

(&:name)是(&:name.to_proc)的缩写,与标签相同。映射{|t| t.name}。加入(' ')

to_proc实际上是用C语言实现的

如下所示:

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

它是标签的简写。映射{|标记|tag .name}。加入(' ')

它的意思是

array.each(&:to_sym.to_proc)