D
David Chelimsky
Hi all,
It seems that Ruby won't allow me to directly access class constants
from instance_eval:
irb(main):001:0> class Foo
irb(main):002:1> BAR = "bar"
irb(main):003:1> def bar
irb(main):004:2> BAR
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo = Foo.new
=> #<Foo:0x10d97f0>
irb(main):008:0> foo.class.constants
=> ["BAR"]
irb(main):009:0> foo.bar
=> "bar"
irb(main):010:0> foo.instance_eval { bar }
=> "bar"
irb(main):011:0> foo.instance_eval { BAR }
NameError: uninitialized constant BAR
from (irb):11
from (irb):11:in `instance_eval'
from (irb):11
Can anybody explain to me why this might be the case (is there a
philosophical reason that this is correct?) and/or some meta-magic
spell I can use to coerce instance_eval into recognizing foo's
constants?
Thanks,
David
It seems that Ruby won't allow me to directly access class constants
from instance_eval:
irb(main):001:0> class Foo
irb(main):002:1> BAR = "bar"
irb(main):003:1> def bar
irb(main):004:2> BAR
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo = Foo.new
=> #<Foo:0x10d97f0>
irb(main):008:0> foo.class.constants
=> ["BAR"]
irb(main):009:0> foo.bar
=> "bar"
irb(main):010:0> foo.instance_eval { bar }
=> "bar"
irb(main):011:0> foo.instance_eval { BAR }
NameError: uninitialized constant BAR
from (irb):11
from (irb):11:in `instance_eval'
from (irb):11
Can anybody explain to me why this might be the case (is there a
philosophical reason that this is correct?) and/or some meta-magic
spell I can use to coerce instance_eval into recognizing foo's
constants?
Thanks,
David