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

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

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


当前回答

Josh Lee的回答几乎是正确的,除了等价的Ruby代码应该如下所示。

class Symbol
  def to_proc
    Proc.new do |receiver|
      receiver.send self
    end
  end
end

not

class Symbol
  def to_proc
    Proc.new do |obj, *args|
      obj.send self, *args
    end
  end
end

使用这段代码,当print [[1,'a'],[2,'b'],[3,'c']].map(&:first)执行时,Ruby将第一个输入[1,'a']分割为1和'a'以给出obj 1和args* 'a'以引起错误,因为Fixnum对象1没有方法self(即:first)。


当[[1,a), (2, ' b '], [3, ' c ']] . map(&:首先)执行;

:first是一个Symbol对象,所以当&:first作为参数被赋给一个map方法时,Symbol#to_proc将被调用。 地图将呼叫信息发送到:first。参数[1,'a']的To_proc,例如:first.to_proc.call([1,'a'])被执行。 Symbol类中的to_proc过程以参数(:first)向数组对象([1,'a'])发送发送消息,例如,[1,'a'].send(:first)被执行。 遍历[[1,'a'],[2,'b'],[3,'c']]对象中的其余元素。

这是一样的执行[[1,' '],[2,' b '], [3, ' c ']]。Map (|e| e.first)表达式。

其他回答

这里:name是指向标记对象的方法名的符号。 当我们将&:name传递给map时,它将把name作为一个proc对象。 简而言之,tags.map(&:name)充当:

tags.map do |tag|
  tag.name
end
tags.map(&:name)

是一样的

tags.map{|tag| tag.name}

&:name只是使用符号作为要调用的方法名。

Ruby中没有&:操作符。你所看到的是&运算符应用于:符号。

在方法参数列表中,&操作符接受其操作数,如果还没有将其转换为Proc对象(通过对其调用to_proc),并将其传递给方法,就像使用了块一样。

my_proc = Proc.new{插入“foo”}

My_method_call (&my_proc) #是相同的: My_method_call {put "foo"}

它的意思是

array.each(&:to_sym.to_proc)

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