A.class_eval(<<-EOF, __FILE__, __LINE__) def method_a puts "calling method_a......" end EOF A.class_eval <<-EOF def method_b puts "calling method_b......" end EOF A.new.method_a A.new.method_b
如上,两种方法定义的区别?
有的,当你带有 FILE, LINE + 1 时,即使是字符串代码块中抛出的异常,也会显示正确的文件中位置,有利于你调试
测试的时候是由 eval 抛出异常,如果不加 _ FILE _ , 和 _ LINE _ 就不会显示文件和行号信息而是直接显示 from (eval) 被 markdown 加斜了...
class A end A.class_eval(<<-EOF, __FILE__, __LINE__) def method_a puts "calling method_a......" end EOF A.new.method_a("bug")
class_eval.rb:3:in method_a': wrong number of arguments (1 for 0) (ArgumentError) from class_eval.rb:15:in
method_a': wrong number of arguments (1 for 0) (ArgumentError) from class_eval.rb:15:in
class A end A.class_eval <<-EOF def method_b puts "calling method_b......" end EOF A.new.method_b("bug")
(eval):1:in method_b': wrong number of arguments (1 for 0) (ArgumentError) from class_eval.rb:16:in
method_b': wrong number of arguments (1 for 0) (ArgumentError) from class_eval.rb:16:in
@hysios @sectic 给力,谢谢!
#2 楼 @sectic
给力!早就觉得有些不同,但是一直没发觉在那里.!!