Project

General

Profile

Actions

Bug #5544

closed

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

Bug #5544: Lookup scope for class variables in class_eval'd procs changed in 1.9.3

Added by jballanc (Joshua Ballanco) about 14 years ago. Updated over 13 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 

Updated by jballanc (Joshua Ballanco) about 14 years ago Actions #1 [ruby-core:40687]

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 Actions #2 [ruby-core:43206]

  • 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 Actions #3

  • 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 Actions #4 [ruby-core:43333]

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 Actions #5 [ruby-core:43379]

I suppose so long as it is documented, the difference is ok. I think this can be closed in that case.

Actions

Also available in: PDF Atom