# Access to constants from instance\_eval

**URL:** <https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886>\
**Category:** Ruby\
**Created:** [February 8, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886 "2007-02-08T12:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 8, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886/1 "2007-02-08T12:47:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 8, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886/2 "2007-02-08T13:19:10Z")

</div>

Hi –

On Thu, 8 Feb 2007, David C. wrote:

> irb(main):006:1\> end  
> 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

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 8, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886/3 "2007-02-08T14:07:15Z")

</div>

On Feb 8, 2007, at 5:46 AM, David C. wrote:

> irb(main):006:1\> end  
> 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:

> > class Foo  
> > BAR = “bar”  
> > def bar  
> > BAR  
> > end  
> > end  
> > =\> nil
> 
> > foo = Foo.new  
> > =\> #Foo:0x11004cc
> 
> > foo.instance\_eval { self.class.const\_get(:BAR) }  
> > =\> “bar”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 8, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/access-to-constants-from-instance-eval/84886/4 "2007-02-08T14:08:15Z")

</div>

Hi,

Am Donnerstag, 08. Feb 2007, 21:18:40 +0900 schrieb removed\_email\_address@domain.invalid:

> > 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
