t6d*_*t6d 14 ruby metaprogramming metaclass dynamic class-method
我被卡住了.我正在尝试动态定义一个类方法,我无法绕过ruby元类模型.考虑以下课程:
class Example
def self.meta; (class << self; self; end); end
def self.class_instance; self; end
end
Example.class_instance.class # => Class
Example.meta.class # => Class
Example.class_instance == Example # => true
Example.class_instance == Example.meta # => false
Run Code Online (Sandbox Code Playgroud)
显然,这两个方法都返回Class的实例.但这两个实例并不相同.他们也有不同的祖先:
Example.meta.ancestors # => [Class, Module, Object, Kernel]
Example.class_instance.ancestors # => [Example, Object, Kernel]
Run Code Online (Sandbox Code Playgroud)
在元类和类实例之间做出改变有什么意义?
我想,我可以send :define_method向元类动态定义一个方法,但是如果我尝试将它发送到类实例它将无法工作.至少我可以解决我的问题,但我仍然想知道它为什么这样工作.
更新2010年3月15日13:40
以下假设是否正确.
我觉得它开始对我有意义了.如果类方法中的self指向特征类,那肯定会限制你的可能性.如果是这样,就无法从类方法中定义实例方法.那是对的吗?
khe*_*lll 11
使用时,动态定义单例方法很简单instance_eval:
Example.instance_eval{ def square(n); n*n; end }
Example.square(2) #=> 4
# you can pass instance_eval a string as well.
Example.instance_eval "def multiply(x,y); x*y; end"
Example.multiply(3,9) #=> 27
Run Code Online (Sandbox Code Playgroud)
至于上面的差异,你会混淆两件事:
您定义的元类是在Ruby社区中称为singelton类或特征类的元类.单例类是可以添加类(单例)方法的类.
至于您尝试使用该class_instance方法定义的类实例,只是类本身,为了证明它,只需尝试向该类添加一个实例方法,Example并检查class_instance您定义的方法是否Example通过检查存在来返回类本身那个方法:
class Example
def self.meta; (class << self; self; end); end
def self.class_instance; self; end
def hey; puts hey; end
end
Example.class_instance.instance_methods(false) #=> ['hey']
Run Code Online (Sandbox Code Playgroud)
无论如何要为你总结,当你想要添加类方法时,只需将它们添加到该元类.至于class_instance方法没用,只需将其删除即可.
无论如何,我建议你阅读这篇文章,以掌握Ruby反射系统的一些概念.
UPDATE
我建议你阅读这篇不错的帖子:Ruby的instance_eval和class_eval的乐趣,不幸的是class_eval,instance_eval它们让人感到困惑,因为它们在某种程度上违背了它们的命名!
Use ClassName.instance_eval to define class methods.
Use ClassName.class_eval to define instance methods.
Run Code Online (Sandbox Code Playgroud)
现在回答你的假设:
如果我有一个调用self.instance_eval并定义方法的实例方法,它只会影响该类的特定实例.
是:
class Foo
def assumption1()
self.instance_eval("def test_assumption_1; puts 'works'; end")
end
end
f1 = Foo.new
f1.assumption1
f1.methods(false) #=> ["test_assumption_1"]
f2 = Foo.new.methods(false) #=> []
Run Code Online (Sandbox Code Playgroud)
如果我有一个调用self.class.instance_eval的实例方法(它与调用class_eval相同)并定义一个方法,它将影响该特定类的所有实例,从而产生一个新的实例方法.
instance_eval在该上下文中,no 将在类本身上定义单例方法(不是实例方法):
class Foo
def assumption2()
self.class.instance_eval("def test_assumption_2; puts 'works'; end")
end
end
f3 = Foo.new
f3.assumption2
f3.methods(false) #=> []
Foo.singleton_methods(false) #=> ["test_assumption_2"]
Run Code Online (Sandbox Code Playgroud)
对于工作代替instance_eval与class_eval上方.
如果我有一个调用instance_eval的类方法并定义一个方法,它将为所有实例生成一个新的实例方法.
不:
class Foo
instance_eval do
def assumption3()
puts 'works'
end
end
end
Foo.instance_methods(false) #=> []
Foo.singleton_methods(false) #=> ["assumption_3"]
Run Code Online (Sandbox Code Playgroud)
这将使单例方法,而不是实例方法.对于工作代替instance_eval与class_eval上方.
如果我有一个类方法在meta/eigen类上调用instance_eval并定义一个方法,它将导致一个类方法.
好吧,那会产生如此复杂的东西,因为它会为单例类添加单例方法,我认为没有任何实际用途.
如果在类上定义方法,则可以在其对象上调用它.这是一个实例方法.
class Example
end
Example.send :define_method, :foo do
puts "foo"
end
Example.new.foo
#=> "foo"
Run Code Online (Sandbox Code Playgroud)
如果在元类上定义方法,则可以在类上调用它.这类似于其他语言中的类方法或静态方法的概念.
class Example
def self.metaclass
class << self
self
end
end
end
Example.metaclass.send :define_method, :bar do
puts "bar"
end
Example.bar
#=> "bar"
Run Code Online (Sandbox Code Playgroud)
存在元类的原因是因为您可以在Ruby中执行此操作:
str = "hello"
class << str
def output
puts self
end
end
str.output
#=> "hello"
"hi".output
# NoMethodError
Run Code Online (Sandbox Code Playgroud)
如您所见,我们定义了一个仅可用于String的一个实例的方法.我们定义此方法的东西称为元类.在方法查找链中,在搜索对象的类之前首先访问元类.
如果我们用类型String的对象替换类型的对象Class,你可以想象为什么这意味着我们只是在特定类上定义一个方法,而不是在所有类上定义.
当前上下文之间的差异self是微妙的,如果您感兴趣,可以阅读更多内容.