Skip to content

Instantly share code, notes, and snippets.

@bachue
Last active December 14, 2015 08:19
Show Gist options
  • Save bachue/5056889 to your computer and use it in GitHub Desktop.
Save bachue/5056889 to your computer and use it in GitHub Desktop.
an example about TOPLEVEL_BINDING
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>
@bachue
Copy link
Author

bachue commented Feb 28, 2013

I don't know why the last error msg is " private method `g'"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment