Last active
August 29, 2015 14:03
-
-
Save yujinakayama/665c949d935e580331e6 to your computer and use it in GitHub Desktop.
#instance_eval does not change the current namespace
This file contains 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
module Foo | |
class Bar | |
BAZ = 'Hello' | |
def print_module_nesting | |
p Module.nesting | |
end | |
end | |
end | |
bar_instance = Foo::Bar.new | |
puts ' Normal '.center(30, '=') | |
bar_instance.print_module_nesting | |
puts | |
puts ' #instance_eval '.center(30, '=') | |
bar_instance.instance_eval do | |
print_module_nesting | |
p Module.nesting | |
p Foo::Bar::BAZ | |
p BAZ | |
end |
This file contains 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
$ ruby --version | |
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] | |
$ ruby instance_eval_module_nesting.rb | |
=========== Normal =========== | |
[Foo::Bar, Foo] | |
======= #instance_eval ======= | |
[Foo::Bar, Foo] | |
[] | |
"Hello" | |
instance_eval_module_nesting.rb:22:in `block in <main>': uninitialized constant BAZ (NameError) | |
from instance_eval_module_nesting.rb:18:in `instance_eval' | |
from instance_eval_module_nesting.rb:18:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment