class_eval和module_eval方法一样, 都是为一个class增加method的。 可以接string和block为参数。 此方法是Ruby的动态特性之一。
例子:
那么:what's the diff between puts y and puts "#{y}" in class_eval ?
我真没想到Matz还到ruby-forum里去混, 姑且认为是Matz本人吧,他给出的答案是:
这有个有意思的例子更加说明了这一点:
关键点是, #{y}是作为string的一部分传给了class_eval. 此时已经是puts first了。 而不是在call hello方法的时候去找y
class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
=> Hello there!
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
=> Hello there!
例子:
class Wes
end
Wes.class_eval <<-END
def hello
puts "#{y}"
puts y
end
END
Wes.new.hello
=> another hi
NameError: undefined local variable or method `y' for #<Wes:0
end
Wes.class_eval <<-END
def hello
puts "#{y}"
puts y
end
END
Wes.new.hello
=> another hi
NameError: undefined local variable or method `y' for #<Wes:0
那么:what's the diff between puts y and puts "#{y}" in class_eval ?
我真没想到Matz还到ruby-forum里去混, 姑且认为是Matz本人吧,他给出的答案是:
用#{y}是在compile time 的时候已经把y的值传给了class_eval.
在运行时, puts “#{y}” 已经变成了 puts “another hi”
而变量y,则因为作用域的不同,外部定义的局部变量y是class_eval无法收到的。
在运行时, puts “#{y}” 已经变成了 puts “another hi”
而变量y,则因为作用域的不同,外部定义的局部变量y是class_eval无法收到的。
这有个有意思的例子更加说明了这一点:
class T
def first
"hello"
end
def second
"chunky bacon"
end
end
y = "first"
T.class_eval <<-END
def hello
puts #{y}
end
END
T.new.hello
=>prints "hello"
def first
"hello"
end
def second
"chunky bacon"
end
end
y = "first"
T.class_eval <<-END
def hello
puts #{y}
end
END
T.new.hello
=>prints "hello"
关键点是, #{y}是作为string的一部分传给了class_eval. 此时已经是puts first了。 而不是在call hello方法的时候去找y