access to constants from instance_eval

  • Thread starter David Chelimsky
  • Start date
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
 
D

dblack

Hi --

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?

foo doesn't have any constants; only Foo does. instance_eval changes
self, but it's still in the same scope. So the constants available
inside the block are the same as those available outside the block.

(You can see foo's instance variables, if any, inside the block, but
that's because the visibility of instance variables depends on
'self'.)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
J

James Edward Gray II

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?

I doubt this is what you are after, but:
=> "bar"

James Edward Gray II
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 08. Feb 2007, 21:18:40 +0900 schrieb (e-mail address removed):
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 [...]
irb(main):011:0> foo.instance_eval { BAR }
NameError: uninitialized constant BAR

foo doesn't have any constants; only Foo does. instance_eval changes
self, but it's still in the same scope. So the constants available
inside the block are the same as those available outside the block.

`Scope' is not neccessarily "self":

irb(main):001:0> class C ; X = "i" ; end
=> "i"
irb(main):002:0> X = "o"
=> "o"
irb(main):003:0> C.instance_eval { X }
=> "o"
irb(main):004:0> C.class_eval { X }
=> "o"
irb(main):005:0> C.instance_eval { self::X }
=> "i"
irb(main):006:0> C.class_eval { self::X }
=> "i"
irb(main):007:0>

Is there a documentation that explains the differences between "scope",
"context", "self" and whatelse?

Bertram
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top