ruby中的“&:”操作符的功能是什么?(复制)

时间:2022-03-30 17:06:11

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

可能的副本:在Ruby中,map(&:name)是什么意思?

I came across a code snippet which had the following

我遇到了一个代码片段,它包含以下内容

a.each_slice(2).map(&:reverse)

I do not know the functionality of &: operator. How does that work?

我不知道&:操作符的功能。这是如何工作的呢?

2 个解决方案

#1


65  

There isn't a &: operator in Ruby. What you are seeing is the & operator applied to a :symbol.

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

In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.

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

my_proc = Proc.new { puts "foo" }

my_method_call(&my_proc) # is identical to:
my_method_call { puts "foo" }

So the question now becomes "What does Symbol#to_proc do?", and that's easy to see in the Rails documentation:

所以现在的问题变成了“#to_proc符号是做什么的?”,这在Rails文档中很容易看到:

Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:

将符号转换为简单的proc,这对于枚举特别有用。例子:

# The same as people.collect { |p| p.name }
people.collect(&:name)

# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)

#2


19  

By prepending & to a symbol you are creating a lambda function that will call method with a name of that symbol on the object you pass into this function. Taking that into account:

通过对符号进行前缀&,您正在创建一个lambda函数,该函数将在传递到该函数的对象上调用带有该符号名称的方法。考虑到这一点:

ar.map(&:reverse)

is roughly equivalent to:

相当于:

ar.map { |element| element.reverse }

#1


65  

There isn't a &: operator in Ruby. What you are seeing is the & operator applied to a :symbol.

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

In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.

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

my_proc = Proc.new { puts "foo" }

my_method_call(&my_proc) # is identical to:
my_method_call { puts "foo" }

So the question now becomes "What does Symbol#to_proc do?", and that's easy to see in the Rails documentation:

所以现在的问题变成了“#to_proc符号是做什么的?”,这在Rails文档中很容易看到:

Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:

将符号转换为简单的proc,这对于枚举特别有用。例子:

# The same as people.collect { |p| p.name }
people.collect(&:name)

# The same as people.select { |p| p.manager? }.collect { |p| p.salary }
people.select(&:manager?).collect(&:salary)

#2


19  

By prepending & to a symbol you are creating a lambda function that will call method with a name of that symbol on the object you pass into this function. Taking that into account:

通过对符号进行前缀&,您正在创建一个lambda函数,该函数将在传递到该函数的对象上调用带有该符号名称的方法。考虑到这一点:

ar.map(&:reverse)

is roughly equivalent to:

相当于:

ar.map { |element| element.reverse }