Project

General

Profile

Actions

Bug #5544

closed

Lookup scope for class variables in class_eval'd procs changed in 1.9.3

Added by jballanc (Joshua Ballanco) over 12 years ago. Updated about 12 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
Backport:
[ruby-core:40649]

Description

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0