Created
June 19, 2010 20:55
-
-
Save trans/445263 to your computer and use it in GitHub Desktop.
Compare Module to Binding Eval
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
class S < Module | |
def initialize | |
extend self | |
end | |
def __binding__ | |
@binding ||= binding | |
end | |
end | |
# Sample 1 | |
s1 = S.new | |
s1.module_eval <<-END | |
X = 10 | |
def x; 10; end | |
END | |
raise unless s1.x == 10 rescue puts "s1.x " + $! | |
raise unless s1::X == 10 rescue puts "s1::X " + $! | |
# Sample 2 | |
s2 = S.new | |
eval(<<-END, s2.__binding__) | |
X = 10 | |
def x; 10; end | |
def y; 20; end | |
END | |
raise unless s2.x == 10 rescue puts "s2.x " + $! | |
raise unless s2.y == 20 rescue puts "s2.y " + $! | |
raise unless s2::X == 10 rescue puts "s2::X " + $! | |
# Sample 3 | |
s3 = S.new | |
raise unless s3.x == 10 rescue puts "s3.x " + $! | |
raise unless s3.y == 20 rescue puts "s3.y " + $! | |
raise unless s3::X == 10 rescue puts "s3::X " + $! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment