我想找出instance_eval和class_eval之间的区别,所以我做了一些工作。在搜索互联网后,我找到了一些东西,但还有一个我无法理解的问题!当我'这样做:
class FindMe
def method_missing method, *args, &block
method = method.to_s
self.class.class_eval <<-EOF, __FILE__, __LINE__
def #{method}
puts "I'm an instance method! I'm #{method}"
end
EOF
send method, *args
end
def self.method_missing method, *args, &block
method = method.to_s
instance_eval <<-HERE, __FILE__, __LINE__
def #{method}
puts "I'm a class method! I'm #{method}"
end
HERE
send method, *args
end
end
FindMe.new.hello
FindMe.hello
我得到了
I'm an instance method! I'm hello
I'm a class method! I'm hello
当我将代码更改为:
时class FindMe
def method_missing method, *args, &block
method = method.to_s
self.class.class_eval <<-EOF, __FILE__, __LINE__
def #{method}
puts "I'm an instance method! I'm #{method}"
end
EOF
send method, *args
end
def self.method_missing method, *args, &block
method = method.to_s
self.class.class_eval <<-HERE, __FILE__, __LINE__
def #{method}
puts "I'm a class method! I'm #{method}"
end
HERE
send method, *args
end
end
FindMe.new.hello
FindMe.hello
我得到相同的输出,任何人都可以告诉我们发生了什么?
答案 0 :(得分:2)
通过class_eval
,您只能通过instance_eval
修改类。看:
▶ class A
▷ def ceval
▷ self.class.class_eval "def on_class ; puts 'On Class' ; end"
▷ end
▷ def ieval
▷ self.instance_eval "def on_instance ; puts 'On Instance' ; end"
▷ end
▷ end
▶ a1 = A.new
#⇒ #<A:0xcf6a87c>
▶ a1.ceval
#⇒ :on_class
▶ a1.ieval
#⇒ :on_instance
▶ a1.on_class
#⇒ On Class
▶ a1.on_instance
#⇒ On Instance
▶ a2 = A.new
#⇒ #<A:0xd0e9f7c>
▶ a2.on_class
#⇒ On Class !!! IT IS DEFINED ON NEWLY CREATED INSTANCE OF A
▶ a2.on_instance
#⇒ NoMethodError: undefined method `on_instance' for #<A:0xd0e9f7c>
后者失败是因为我们在实例on_instance
上声明了a1
方法,而a2
对此一无所知。
答案 1 :(得分:1)
请参阅此示例,并尝试向您自己解释:
class Foo
def a
self.class.class_eval <<-EOS
def x
'This is an instance method.'
end
EOS
end
def b
self.instance_eval <<-EOS
def y
'This is a singleton method.'
end
EOS
end
end
foo1 = Foo.new
foo2 = Foo.new
foo1.a
foo1.b
foo1.x #=> This is an instance method.
foo1.y #=> This is a singleton method.
foo2.x #=> This is an instance method.
foo2.y #=> NoMethodError