instance_eval and Class Variables

T

Tim Bates

Hi again,
The previous email had "instance_eval" in the title, because I meant to
include the contents of this mail in it but then didn't. It actually
used module_eval, although it makes no difference. This one is a
separate problem:

irb(main):001:0> class Klass
irb(main):002:1> @@foo = "Foo!"
irb(main):003:1> end
=> "Foo!"
irb(main):004:0> Klass.instance_eval{ puts @@foo }
NameError: uninitialized class variable @@foo in Object
from (irb):4
from (irb):4:in `instance_eval'
from (irb):4:in `instance_eval'
from (irb):4
irb(main):005:0> Klass.module_eval{ puts @@foo }
NameError: uninitialized class variable @@foo in Object
from (irb):5
from (irb):5:in `module_eval'
from (irb):5:in `module_eval'
from (irb):5
irb(main):006:0> Klass.new.instance_eval{ puts @@foo }
NameError: uninitialized class variable @@foo in Object
from (irb):6
from (irb):6:in `instance_eval'
from (irb):6:in `instance_eval'
from (irb):6

Why can't I get at a class variable from instance_eval? It works fine
for instance variables:

irb(main):001:0> class Klass
irb(main):002:1> def initialize
irb(main):003:2> @instance_foo = "InstanceFoo!"
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> Klass.new.instance_eval{ puts @instance_foo }
InstanceFoo!
=> nil

BTW, both this email and the previous one are with 1.8.0-preview3.

Tim Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top