Ruby instance_eval 与 class_eval

@(Ruby)[class_eval, instance_eval]
irb模式下,instance是无法调用class_eval方法的,而在 rails c模式下是可以的。

class A
end
A.instance_eval do
  def test_method1
    p 'this is from A.instance_eval'
  end
end
A.class_eval do
  def test_method2
    p 'this is fromt A.class_eval'
  end
end

A.test_method1 # "this is from A.instance_eval"
A.test_method2 # NoMethodError: undefined method `test_method2' for A:Class

class B; end
b = B.new
b.instance_eval do
  def test_method1
    p 'this is from b.instance_eval'
  end
end
b.class_eval do
  def test_method2
    p 'this is from b.class_eval'
  end
end

b.test_method1 # "this is from b.instance_eval"
b.test_method2 # "this is from b.class_eval"
[32] pry(main)> b.singleton_class.instance_methods.grep /test_/
=> [:test_method1, :test_method2]
# 在实例上调用`class_eval`和`instance_eval`效果相同
[33] pry(main)>
[38] pry(main)> $ b.class_eval
def class_eval(*args, &block)
  singleton_class.class_eval(*args, &block)
end
[39] pry(main)>

结论

instance_eval class_eval
class A 生成A父类的实例方法,类似于A的类方法,此时会改变A.singleton_class 生成A的实例方法,此时会改变A本身的结构,但是A却不能调用该方法
instance b 生成b父类的实例方法,类似于b的类方法 instance_eval效果相同

注意:b是实例,不存在类方法。就只有实例方法,区别在于 instance 为父类添加,class 为子类添加

你可能感兴趣的:(RUBY)