Last active
December 14, 2015 20:19
-
-
Save samsm/5143270 to your computer and use it in GitHub Desktop.
Ruby instance_eval scope issue I don't understand.
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 Bar | |
module Baz | |
end | |
def hi_there | |
puts 'Hi!!' | |
end | |
end | |
class Foo | |
include Bar | |
def initialize(&block) | |
puts "Baz is available in this context: #{Baz.inspect}" | |
instance_eval(&block) | |
end | |
end | |
Foo.new do | |
hi_there | |
puts "Global reference works ok: #{Bar::Baz.inspect}" | |
puts "Why isn't Baz found? #{Baz.inspect}" # NameError: uninitialized constant Baz | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
instance_eval
only changesself
. All non-instance variables (incl classes/modules) are not affected and retain the binding of when the block was defined.