Last active
December 14, 2015 08:19
-
-
Save bachue/5056889 to your computer and use it in GitHub Desktop.
an example about TOPLEVEL_BINDING
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
eval 'def f; "hello world"; end' | |
end | |
A.new.f # => "hello world" | |
A.f # NoMethodError: undefined method `f' for A:Class | |
f # NameError: undefined local variable or method `f' for main:Object | |
class A | |
eval 'self', TOPLEVEL_BINDING # => main | |
end | |
class A | |
eval 'def g; "hello world"; end', TOPLEVEL_BINDING | |
end | |
g # => "hello world" | |
A.new.g # NoMethodError: private method `g' called for #<A:0x007f81af845e58> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know why the last error msg is " private method `g'"