I have a dummy object that always yield itself:
x = Object.new; x.define_singleton_method(:method_missing) { |*| x }
x.foo.bar.baz == x # => true
I wanted to simplify it, but suddenly it doesn't work anymore:
x = Object.new; def x.method_missing(*) x end
x.foo # raises SystemStackError, stack level too deep
Why is a SystemStackError raised in that last example?
Found it!
The
defcreates a new scope for local variables, whereas the closure keeps local variables.Per comment, to fix it you could use
self, or for a shorter declaration: