伙计们。我创建了一个类:
class A
def initialize &b
instance_eval &b
end
def method_missing method_id, *args
self.define_method(method_id) { puts args.first }
end
end
b = A.new { new_method "oops" }
但是不行
SystemStackError: stack level too deep
为什么?
最佳答案
define_method 没有为 A 的instance 定义,所以当你再次调用 self.define_method 时,cal method_missing 又一次 => 堆栈溢出。
你需要做类似的事情
class A
def initialize &b
instance_eval &b
end
def method_missing(method_id, *args)
self.class.instance_eval do
define_method(method_id) { debugger; puts args.first }
end
end
end
关于ruby - instance_eval、define_method 和 method_missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7471087/