Skip to content

Instantly share code, notes, and snippets.

@jballanc
Created October 31, 2011 21:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jballanc/1328983 to your computer and use it in GitHub Desktop.
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
# 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