Created
October 31, 2011 21:17
-
-
Save jballanc/1328983 to your computer and use it in GitHub Desktop.
Different behavior of class_eval between Ruby 1.9.2 and Ruby 1.9.3
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
# Compare the results running under Ruby 1.9.2 and Ruby 1.9.3 | |
class Foo | |
def initialize | |
@@value = "Set from Foo initialize" | |
end | |
def report | |
@@value | |
end | |
end | |
class Bar | |
def initialize | |
@@value = "Set from Bar initialize" | |
end | |
def report | |
@@value | |
end | |
def monkey | |
Foo.class_eval do | |
def set(value) | |
@@value = value | |
end | |
end | |
end | |
end | |
b = Bar.new | |
b.monkey | |
f = Foo.new | |
puts "Before monkeying:" | |
puts "Bar's class var: #{b.report}" | |
puts "Foo's class var: #{f.report}" | |
f.set("Set through Monkey") | |
puts "After monkeying:" | |
puts "Bar's class var: #{b.report}" | |
puts "Foo's class var: #{f.report}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment