Issue #16467 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Rejected

Class variables need to be in the proper class context.
The following code raises a `NameError` too.
```ruby
class FooClass::BarClass
  @@bar # NameError (uninitialized class variable @@bar in FooClass::BarClass)
end
```

You have to use `class_eval`.

```ruby
foo.class.class_eval("@@bar")
# or
FooClass.class_eval("@@bar")
```

----------------------------------------
Bug #16467: Fetching a class variable with instance_eval fails with NameError
https://bugs.ruby-lang.org/issues/16467#change-83558

* Author: barrettkingram (Barrett Ingram)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
Trying to fetch a class variable using `instance_eval` fails with `uninitialized class variable @variable_name in Object (NameError)`.

Reproduction program:
``` ruby
class FooClass
  def initialize
    @foo = "foo"
    @@bar = "bar"
  end

  def get_bar
    @@bar
  end
end

foo = FooClass.new
puts foo.instance_eval("@foo") # prints "foo"
puts foo.get_bar # prints "bar"
puts foo.instance_eval("@@bar") # raises uninitialized class variable @@bar in Object (NameError)
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>