avy*_*avy 10 ruby oop metaprogramming
我在红宝石中使用元编程,我有一个问题.我有一节课:
class Klass
class << self
@x = "yeah"
end
end
b = Klass.new
a = class << Klass; self; end
a.instance_eval "@x" #=> yeah
Klass.instance_eval "@x" #=> nil
Run Code Online (Sandbox Code Playgroud)
为什么?在变量中a我有一个单例类,对吗?并且Klass.instance_eval在单例类的上下文中执行:
Klass.instance_eval "def yeah; puts 10; end"
Klass.yeah #=> 10
Run Code Online (Sandbox Code Playgroud)
另外,Klass在翻译中指出课程的背景,是吗?并a指出单例类的上下文?哪个表明a.class_eval和a.instance_eval?我做:
a.instance_eval "def pops; puts 0; end"
a.class_eval "def popsx; puts 1; end"
a.pops #=> 0
a.popsx # FAIL
Klass.pops # FAIL
Klass.popsx #=> 1
b.pops; b.popsx # DOUBLE FAIL
Run Code Online (Sandbox Code Playgroud)
而且我不明白这一点.谢谢!
首先,虽然有些人似乎使用特征类,但单例类是更常见的术语。Singleton 类包含 Ruby 中对象的特定于对象的行为。除了该单例类所属的原始对象之外,您无法创建该类的其他实例。
谈到在不同类型的 eval 中定义方法,本文instance_eval介绍了和中定义的方法的良好规则class_eval:
Run Code Online (Sandbox Code Playgroud)Use ClassName.instance_eval to define class methods. Use ClassName.class_eval to define instance methods.
这几乎描述了情况。
有大量关于类的文章,这些类是 Class 类的实例,它们的单例类是 Class 类的子类,以及其他一些疯狂的东西(与问题没有太大关系)。但是,由于您的问题可以轻松应用于常规对象及其类(并且这使得事情更容易解释),我决定删除所有内容(不过,您仍然可以在答案的修订历史记录中看到这些内容)。
让我们看看常规类和该类的实例,看看它们是如何工作的:
class A; end
a = A.new
Run Code Online (Sandbox Code Playgroud)
不同类型的 eval 中的方法定义:
# define instance method inside class context
A.class_eval { def bar; 'bar'; end }
puts a.bar # => bar
puts A.new.bar # => bar
# class_eval is equivalent to re-opening the class
class A
def bar2; 'bar2'; end
end
puts a.bar2 # => bar2
puts A.new.bar2 # => bar2
Run Code Online (Sandbox Code Playgroud)
定义特定于对象的方法:
# define object-specific method in the context of object itself
a.instance_eval { def foo; 'foo'; end }
puts a.foo # => foo
# method definition inside instance_eval is equivalent to this
def a.foo2; 'foo2'; end
puts a.foo2 # => foo2
# no foo method here
# puts A.new.foo # => undefined method `foo' for #<A:0x8b35b20>
Run Code Online (Sandbox Code Playgroud)
现在让我们看看对象的单例类a:
# singleton class of a is subclass of A
p (class << a; self; end).ancestors
# => [A, Object, Kernel, BasicObject]
# define instance method inside a's singleton class context
class << a
def foobar; 'foobar'; end;
end
puts a.foobar # => foobar
# as expected foobar is not available for other instances of class A
# because it's instance method of a's singleton class and a is the only
# instance of that class
# puts A.new.foobar # => undefined method `foobar' for #<A:0x8b35b20>
# same for equivalent class_eval version
(class << a; self; end).class_eval do
def foobar2; 'foobar2'; end;
end
puts a.foobar2 # => foobar2
# no foobar2 here as well
# puts A.new.foobar2 # => undefined method `foobar2' for #<A:0x8b35b20>
Run Code Online (Sandbox Code Playgroud)
现在让我们看看实例变量:
# define instance variable for object a
a.instance_eval { @x = 1 }
# we can access that @x using same instance_eval
puts a.instance_eval { @x } # => 1
# or via convenient instance_variable_get method
puts a.instance_variable_get(:@x) # => 1
Run Code Online (Sandbox Code Playgroud)
现在到里面的实例变量class_eval:
# class_eval is instance method of Module class
# so it's not available for object a
# a.class_eval { } # => undefined method `class_eval' for #<A:0x8fbaa74>
# instance variable definition works the same inside
# class_eval and instance_eval
A.instance_eval { @y = 1 }
A.class_eval { @z = 1 }
# both variables belong to A class itself
p A.instance_variables # => [:@y, :@z]
# instance variables can be accessed in both ways as well
puts A.instance_eval { @y } # => 1
puts A.class_eval { @z } # => 1
# no instance_variables here
p A.new.instance_variables # => []
Run Code Online (Sandbox Code Playgroud)
现在,如果您将 class 替换A为 class ,将Classobject替换a为 object Klass(在这种特殊情况下只不过是 class 的实例Class),我希望您能得到对您问题的解释。如果您还有一些,请随时询问。
| 归档时间: |
|
| 查看次数: |
245 次 |
| 最近记录: |