`&method(:method_ name)` 在 ruby 中是什么意思? [英] What does `&method(:method_ name)` mean in ruby?
问题描述
我试图创建一个具有私有类方法的类.我希望这个私有类方法可以在实例方法中使用.
以下是我的第一次尝试:
类动物类<<自己def public_class_greeter(名称)private_class_greeter(姓名)结尾私人的def private_class_greeter(名称)puts "#{name} 迎接私有类方法"结尾结尾def public_instance_greeter(名称)self.class.private_class_greeter(姓名)结尾结尾
Animal.public_class_greeter('John')
工作正常,打印 John 问候私有类方法
.
然而,Animal.new.public_instance_greeter("John")
抛出错误:NoMethodError: private method 'private_class_greeter' called for Animal:Class
.
这是预期的,因为调用 self.class.private_class_greeter
与 Animal.private_class_greeter
相同,这显然会引发错误.
在搜索如何解决此问题后,我想出了以下代码,它可以完成工作:
类动物类<<自己def public_class_greeter(名称)private_class_greeter(姓名)结尾私人的def private_class_greeter(名称)puts "#{name} 迎接私有类方法"结尾结尾定义方法:public_instance_greeter,&method(:private_class_greeter)结尾
我不太明白这里发生了什么:&method(:private_class_greeter)
.
你能解释一下这是什么意思吗?
如果我要替换:
define_method :public_instance_greeter, &method(:private_class_greeter)
与:
def public_instance_greeterXYZ结尾
那么,代替XYZ
的内容应该是什么?
Ruby 如何解析&method(:private_class_greeter)
?
表达式&method(:private_class_greeter)
是
- 方法调用的值
method(:private_class_greeter)
- 以
&
运算符为前缀.
method
方法有什么作用?
方法
方法在当前上下文中查找指定的方法名称,并返回一个表示它的 Method
对象.irb
中的示例:
def foo酒吧"结尾my_method = 方法(:foo)#=>#<方法:对象#foo>
一旦你有了这个方法,你就可以用它做各种事情:
my_method.call#=>酒吧"my_method.source_location # 为您提供定义方法的文件和行#=>["(irb)", 5]# 等等.
<小时>
&
运算符的用途是什么?
&
运算符用于将 Proc
作为块传递给方法,该方法需要一个块传递给它.它还隐式调用您传入的值的 to_proc
方法,以便将不是 Proc
的值转换为 Proc
.>
Method
类实现了 to_proc
—它将方法的内容作为 Proc
返回.因此,您可以在 Method
实例前面加上 &
并将其作为块传递给另一个方法:
def call_block屈服结尾call_block &my_method # 与 `call_block &my_method.to_proc` 相同#=>酒吧"
define_method
方法恰好采用了一个块,其中包含正在定义的新方法的内容.在您的示例中,&method(:private_class_greeter)
将现有的 private_class_greeter
方法作为一个块传入.
&:symbol
是这样工作的吗?
是的.Symbol
实现了 to_proc
以便您可以像这样简化代码:
["foo", "bar"].map(&:upcase)#=>[FOO",酒吧"]# 这相当于:["foo", "bar"].map { |item|item.upcase }# 因为:upcase.to_proc# 返回这个过程:过程 { |val|val.send(:upcase) }
<小时>
如何复制&method(:private_class_greeter)
?
你可以传入一个调用目标方法的块:
define_method :public_instance_greeter do |name|self.class.send(:private_class_greeter, name)结尾
当然,那你就不需要再使用define_method
了,这就产生了与他的回答:
def public_instance_greeter(name)self.class.send(:private_class_greeter, name)结尾
I was trying to create a class that has a private class method. I want this private class method available to be used inside an instance method.
The following was my first attempt:
class Animal
class << self
def public_class_greeter(name)
private_class_greeter(name)
end
private
def private_class_greeter(name)
puts "#{name} greets private class method"
end
end
def public_instance_greeter(name)
self.class.private_class_greeter(name)
end
end
Animal.public_class_greeter('John')
works fine, printing John greets private class method
.
However, Animal.new.public_instance_greeter("John")
throws an error: NoMethodError: private method 'private_class_greeter' called for Animal:Class
.
That is expected, as the invocation self.class.private_class_greeter
is same as Animal.private_class_greeter
, which obviously throws an error.
After searching on how this can be fixed, I came up with the following code, that does the job:
class Animal
class << self
def public_class_greeter(name)
private_class_greeter(name)
end
private
def private_class_greeter(name)
puts "#{name} greets private class method"
end
end
define_method :public_instance_greeter, &method(:private_class_greeter)
end
I don't exactly understand what is happening here: &method(:private_class_greeter)
.
Could you please explain what does this mean?
If I were to replace:
define_method :public_instance_greeter, &method(:private_class_greeter)
with:
def public_instance_greeter
XYZ
end
then, what should be the content in place of XYZ
?
How does Ruby parse &method(:private_class_greeter)
?
The expression &method(:private_class_greeter)
is
- the value of the method call
method(:private_class_greeter)
- prefixed with the
&
operator.
What does the method
method do?
The method
method looks up the specified method name in the current context and returns a Method
object that represents it. Example in irb
:
def foo
"bar"
end
my_method = method(:foo)
#=> #<Method: Object#foo>
Once you have this method, you can do various things with it:
my_method.call
#=> "bar"
my_method.source_location # gives you the file and line the method was defined on
#=> ["(irb)", 5]
# etc.
What is the &
operator for?
The &
operator is used to pass a Proc
as a block to a method that expects a block to be passed to it. It also implicitly calls the to_proc
method on the value you pass in, in order to convert values that are not Proc
into a Proc
.
The Method
class implements to_proc
— it returns the contents of the method as a Proc
. Therefore, you can prefix a Method
instance with &
and pass it as a block to another method:
def call_block
yield
end
call_block &my_method # same as `call_block &my_method.to_proc`
#=> "bar"
The define_method
method just happens to take a block with the contents of the new method that is being defined. In your example, &method(:private_class_greeter)
passes in the existing private_class_greeter
method as a block.
Is this how &:symbol
works?
Yes. Symbol
implements to_proc
so that you can simplify your code like this:
["foo", "bar"].map(&:upcase)
#=> ["FOO", "BAR"]
# this is equivalent to:
["foo", "bar"].map { |item| item.upcase }
# because
:upcase.to_proc
# returns this proc:
Proc { |val| val.send(:upcase) }
How can I replicate &method(:private_class_greeter)
?
You can pass in a block that calls the target method:
define_method :public_instance_greeter do |name|
self.class.send(:private_class_greeter, name)
end
Of course, then you don't need to use define_method
anymore, which results in the same solution Eric mentioned in his answer:
def public_instance_greeter(name)
self.class.send(:private_class_greeter, name)
end
这篇关于`&amp;method(:method_ name)` 在 ruby 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!