ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:96586] [Ruby master Bug#16467] Fetching a class variable with instance_eval fails with NameError
       [not found] <redmine.issue-16467.20191230004429@ruby-lang.org>
@ 2019-12-30  0:44 ` barrettkingram
  2019-12-30  9:05 ` [ruby-core:96591] " nobu
  1 sibling, 0 replies; 2+ messages in thread
From: barrettkingram @ 2019-12-30  0:44 UTC (permalink / raw)
  To: ruby-core

Issue #16467 has been reported by barrettkingram (Barrett Ingram).

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

* Author: barrettkingram (Barrett Ingram)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:96591] [Ruby master Bug#16467] Fetching a class variable with instance_eval fails with NameError
       [not found] <redmine.issue-16467.20191230004429@ruby-lang.org>
  2019-12-30  0:44 ` [ruby-core:96586] [Ruby master Bug#16467] Fetching a class variable with instance_eval fails with NameError barrettkingram
@ 2019-12-30  9:05 ` nobu
  1 sibling, 0 replies; 2+ messages in thread
From: nobu @ 2019-12-30  9:05 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-30  9:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-16467.20191230004429@ruby-lang.org>
2019-12-30  0:44 ` [ruby-core:96586] [Ruby master Bug#16467] Fetching a class variable with instance_eval fails with NameError barrettkingram
2019-12-30  9:05 ` [ruby-core:96591] " nobu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).