类和对象的 Ruby 单例方法 [英] Ruby Singleton methods for class and objects
问题描述
我正在学习 Ruby 单例,我找到了一些方法来定义和获取类和对象单例方法的列表.
I am learning Ruby Singletons and i found some ways to define and get list of Class and Object singleton methods.
定义类单例方法的方法:
class MyClass
def MyClass.first_sing_method
'first'
end
def self.second_sing_method
'second'
end
class << self
def third_sing_method
'third'
end
end
class << MyClass
def fourth_sing_method
'fourth'
end
end
end
def MyClass.fifth_sing_method
'fifth'
end
MyClass.define_singleton_method(:sixth_sing_method) do
'sixth'
end
获取类单例方法列表的方法:
#get singleton methods list for class and it's ancestors
MyClass.singleton_methods
#get singleton methods list for current class only
MyClass.methods(false)
对象单例方法
定义对象单例方法的方法
class MyClass
end
obj = MyClass.new
class << obj
def first_sing_method
'first'
end
end
def obj.second_sing_method
'second'
end
obj.define_singleton_method(:third_sing_method) do
'third'
end
获取对象单例方法列表的方法
#get singleton methods list for object and it's ancestors
obj.singleton_methods
#get singleton methods list for current object only
obj.methods(false)
还有其他方法可以做到这一点吗?
推荐答案
首先,列出单例方法的方式是使用 singleton_methods
.methods
方法返回对象的公共方法和受保护方法的名称列表.此外,它在 Object<中定义/code>
类.尝试 extend
ing 一个实例.这是最优雅的方式之一,因为它支持代码重用并且在我看来非常面向对象:
First of all, the way to list singleton methods is with singleton_methods
. The methods
method returns a list of the names of public and protected methods of the object. Also, it is defined in the Object
class. Try extend
ing an instance. It is one of the most elegant ways, as it supports code reuse and seems to me very object-oriented:
class Foo
def bar
puts "Hi"
end
end
module Bar
def foo
puts "Bye"
end
end
f = Foo.new
f.bar
#=> hi
f.extend Bar
f.foo
#=> bye
f.methods(false)
#=> []
# the module we extended from is not a superclass
# therefore, this must be empty, as expected
f.singleton_methods
#=> ["foo"]
# this lists the singleton method correctly
g = Foo.new
g.foo
#=> NoMethodError
编辑:在您询问为什么 methods(false)
在这种情况下不返回任何内容的评论中.通读 C 代码后,似乎:
Edit: In the comment you asked why methods(false)
returns nothing in this case. After reading through the C code it seems that:
methods
返回对象的所有可用方法(也包括模块中的方法)singleton_methods
返回对象的所有单例方法(也包括模块中的方法)(文档)singleton_methods(false)
返回对象的所有单例方法,但不在包含的模块中声明的方法methods(false)
应该通过调用singleton_methods
返回单例方法,但它也传递参数false
> 给它;这是错误还是功能 - 我不知道
methods
returns all the methods available for the object (also the ones in included modules)singleton_methods
returns all the singleton methods for the object (also the ones in included modules) (documentation)singleton_methods(false)
returns all the singleton methods for the object, but not those declared in included modulesmethods(false)
supposedly returns the singleton methods by callingsingleton_methods
, but it also passes the parameterfalse
to it; whether this is a bug or a feature - I do not know
希望这能稍微澄清这个问题.底线:调用singleton_methods
,似乎更可靠.
Hopefully, this clarifies the issue a little. Bottom line: call singleton_methods
, seems more reliable.
这篇关于类和对象的 Ruby 单例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!