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

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

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


当前回答

(&: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

Map (&:name)接受一个可枚举对象(在您的例子中是标签),并为每个元素/标签运行name方法,输出该方法的每个返回值。

这是一个简写

array.map { |element| element.name }

返回元素(标签)名称的数组

这里发生了两件事,理解这两件事很重要。

正如在其他回答中所描述的,正在调用Symbol#to_proc方法。

但是符号上调用to_proc的原因是因为它被作为块参数传递给map。将&放在方法调用中的参数前面会导致它以这种方式传递。这适用于任何Ruby方法,而不仅仅是带有符号的映射。

def some_method(*args, &block)
  puts "args: #{args.inspect}"
  puts "block: #{block.inspect}"
end

some_method(:whatever)
# args: [:whatever]
# block: nil

some_method(&:whatever)
# args: []
# block: #<Proc:0x007fd23d010da8>

some_method(&"whatever")
# TypeError: wrong argument type String (expected Proc)
# (String doesn't respond to #to_proc)

Symbol被转换为Proc,因为它是作为块传入的。我们可以通过尝试传递一个不带&号的proc给.map来显示这一点:

arr = %w(apple banana)
reverse_upcase = proc { |i| i.reverse.upcase }
reverse_upcase.is_a?(Proc)
=> true

arr.map(reverse_upcase)
# ArgumentError: wrong number of arguments (1 for 0)
# (map expects 0 positional arguments and one block argument)

arr.map(&reverse_upcase)
=> ["ELPPA", "ANANAB"]

即使它不需要转换,方法也不知道如何使用它,因为它期望一个块参数。给它传递&给.map它所期望的块。

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)表达式。

同时让我们也注意到& #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!' ) }