Bug #5544
closedLookup scope for class variables in class_eval'd procs changed in 1.9.3
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
Updated by jballanc (Joshua Ballanco) about 14 years ago
This inconsistency was introduced in r31215:
* vm_insnhelper.h (COPY_CREF): should copy the NODE_FL_CREF_PUSHED_BY_EVAL flag to hide constants from methods defined by class_eval. [ruby-dev:43365] 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.
Updated by ko1 (Koichi Sasada) over 13 years ago
- Category set to core
- Status changed from Open to Assigned
- Assignee set to shugo (Shugo Maeda)
Updated by shugo (Shugo Maeda) over 13 years ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r35057.
Joshua, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
- vm_eval.c (rb_mod_module_eval): fix the documentation of
class_eval to mention class variable lookup. [ruby-core:40649]
[Bug #5544]
Updated by shugo (Shugo Maeda) over 13 years ago
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.
Updated by jballanc (Joshua Ballanco) over 13 years ago
I suppose so long as it is documented, the difference is ok. I think this can be closed in that case.