From: Shugo Maeda Date: 2012-03-16T14:42:10+09:00 Subject: [ruby-core:43333] [ruby-trunk - Bug #5544] Lookup scope for class variables in class_eval'd procs changed in 1.9.3 Issue #5544 has been updated by Shugo Maeda. Joshua Ballanco wrote: > While I prefer the behavior in 1.9.3 (it is more consistent with constant lookup rules), the inconsistency between 1.9.2 and 1.9.3 is unsettling. It is intended, so I've fixed the documentation of class_eval. Please file a feature request ticket if you don't like it. ---------------------------------------- Bug #5544: Lookup scope for class variables in class_eval'd procs changed in 1.9.3 https://bugs.ruby-lang.org/issues/5544#change-24642 Author: Joshua Ballanco Status: Closed Priority: Normal Assignee: Shugo Maeda Category: core Target version: ruby -v: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0] When using class_eval with a proc, references to class variables scoped to the receiver in 1.9.2, but in 1.9.3 class variables are now lexically scoped to the environment of the proc. For example, this code: 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}" Running under 1.9.2-p290: Before monkeying: Bar's class var: Set from Bar initialize Foo's class var: Set from Foo initialize After monkeying: Bar's class var: Set from Bar initialize Foo's class var: Set through Monkey Running under 1.9.3-p0: Before monkeying: Bar's class var: Set from Bar initialize Foo's class var: Set from Foo initialize After monkeying: Bar's class var: Set through Monkey Foo's class var: Set from Foo initialize -- http://bugs.ruby-lang.org/