This question deals with Ruby 1.9.3 specifically:
p defined?(a)
p binding.eval "defined?(a)"
b = lambda { |x| x }
p b.binding.eval "defined?(a)" # This prints "local-variable"
p defined?(a) # This prints nil!
a = 2
p defined?(a)
p b.binding.eval "defined?(a)"
What confuses me is line four. I'm not sure why this prints "local-variable" rather than nil. This seems to imply that lambda is somehow "looking farther." (I figure defined?
being an operator has something to do with this.)
Also, although the binding says that it is defined, attempting to use it like so:
p b.binding.eval "a"
before the assignment on line 6 results in a NameError.
EDIT: I have tested this on
- 1.9.3-362
- 1.9.3-374
- 2.0.0-preview2
I get the same behavior in all of the cases.