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

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

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


当前回答

tags.map(&:name)

是一样的

tags.map{|tag| tag.name}

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

其他回答

同时让我们也注意到& #to_proc魔法可以对任何类工作,而不仅仅是符号。很多ruby会选择在Array类上定义#to_proc:

class Array
  def to_proc
    proc { |receiver| receiver.send *self }
  end
end

# And then...

[ 'Hello', 'Goodbye' ].map &[ :+, ' world!' ]
#=> ["Hello world!", "Goodbye world!"]

&的工作原理是在其操作数上发送to_proc消息,在上面的代码中,操作数属于Array类。因为我在Array上定义了#to_proc方法,所以这一行变成:

[ 'Hello', 'Goodbye' ].map { |receiver| receiver.send( :+, ' world!' ) }

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

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

tags.map do |tag|
  tag.name
end

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的快捷键。To_proc,其中:name。to_proc返回一个Proc(类似于lambda,但不完全相同),当以对象作为(第一个)参数调用时,调用该对象的name方法。

其次,当& in def foo(&block)…end将传递给foo的块转换为Proc,当应用到Proc时则相反。

因此,&:名字。To_proc是一个以一个对象作为参数并调用该对象上的name方法的块,即{|o| o.name}。