class_eval和module_eval方法一样, 都是为一个class增加method的。 可以接string和block为参数。 此方法是Ruby的动态特性之一。
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.class_eval(a)
puts Thing.new.hello()
=> Hello there!
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.class_eval(a)
puts Thing.new.hello()
=> Hello there!
转载于:https://blog.51cto.com/gypsyer/162293