# Scope of constants in instance\_eval

**URL:** <https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742>\
**Category:** ruby-talk\
**Created:** [10 October 2009 02:44 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742 "2009-10-10T02:44:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [10 October 2009 02:44 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/1 "2009-10-10T02:44:56Z")

</div>

so If I do\<br\>

class MyObj \< Module\<br\>  
&nbsp;&nbsp;&nbsp;CONST = 'xxx'\<br\>

&nbsp;&nbsp;&nbsp;def initalialize(&block)\<br\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(&block)\<br\>  
&nbsp;&nbsp;&nbsp;end\<br\>  
end\<br\>  
\<br\>  
MyObj.new do\<br\>  
&nbsp;&nbsp;&nbsp;&nbsp;puts("CONST = #{CONST}")\<br\>  
end\<br\>  
\<br\>  
\<br\>  
# this will print\<br\>  
unitialized constant CONST\<br\>  
\<br\>  
So how do I create a "constant" ie: attribute with leading upper case  
char that is resolvable within "instance\_eval()" for a particular  
instance of a class that defines the constant or subclass of it or class  
that includes a module that defines the constant?

I tried overriding "const\_missing" but as a static method on class  
Object I see no way to determine the "self" that was active when the  
constant was not resolved, If so I could then make it happen, by  
examinig the "self" for it's class etc and look for the constants in  
them 🙂

Kind of like i'd like to be able to get caller[0].binding.self inside  
"const\_missing"

This so it can be like Java to get "this.CONST"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [10 October 2009 03:48 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/2 "2009-10-10T03:48:58Z")

</div>

This is a bit of a strange problem. Ruby 1.8 resolves constants lexically.

So you are actually referring to ::CONST there.

In Ruby 1.9, this is different. In Ruby 1.9, Constants are resolved by ´self´.  
So, in short:

&nbsp;&nbsp;&nbsp;class A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;A.new.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B  
&nbsp;&nbsp;&nbsp;end

crashes in 1.8, but resolves B as A::B in 1.9. Some people hate that change  
in behaviour, I like it.

It doesn't directly answer your question, but I hope it gives you enough insight to find a way to do what you want.

Regards,  
Florian

> **···**
>
> On Oct 10, 2009, at 12:14 AM, Peter Pk wrote:
> 
> > so If I do\<br\>
> > 
> > class MyObj \< Module\<br\>  
> > &nbsp;&nbsp;CONST = 'xxx'\<br\>
> > 
> > &nbsp;&nbsp;def initalialize(&block)\<br\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(&block)\<br\>  
> > &nbsp;&nbsp;end\<br\>  
> > end\<br\>  
> > \<br\>  
> > MyObj.new do\<br\>  
> > &nbsp;&nbsp;&nbsp;puts("CONST = #{CONST}")\<br\>  
> > end\<br\>  
> > \<br\>  
> > # this will print\<br\>  
> > unitialized constant CONST\<br\>  
> > \<br\>  
> > So how do I create a "constant" ie: attribute with leading upper case  
> > char that is resolvable within "instance\_eval()" for a particular  
> > instance of a class that defines the constant or subclass of it or class  
> > that includes a module that defines the constant?
> > 
> > I tried overriding "const\_missing" but as a static method on class  
> > Object I see no way to determine the "self" that was active when the  
> > constant was not resolved, If so I could then make it happen, by  
> > examinig the "self" for it's class etc and look for the constants in  
> > them 🙂
> > 
> > Kind of like i'd like to be able to get caller[0].binding.self inside  
> > "const\_missing"
> > 
> > This so it can be like Java to get "this.CONST"  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Florian Gilcher
> 
> smtp: flo@andersground.net  
> jabber: Skade@jabber.ccc.de  
> gpg: 533148E2

---

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [10 October 2009 12:48 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/3 "2009-10-10T12:48:40Z")

</div>

Thanks - actually it does answer the question, but it also answers that  
I have to wait until Ruby 1.9 for the "fix"

that is unless there is a workaround way (until 1.9) to

doInConstanScopeOf [Module] do  
# so instead of 'Module::CONST' one can do 'CONST'  
end

so one could do

def doIt(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;doInConstanScopeOf AModule &block  
end

and explicitly define the constant scope to be the scope of "self.class"

so when will the fabled 1.9 be available 🙂

Florian Gilcher wrote:

> **···**
>
> > This is a bit of a strange problem. Ruby 1.8 resolves constants  
> > lexically.
> > 
> > So you are actually referring to ::CONST there.
> > 
> > In Ruby 1.9, this is different. In Ruby 1.9, Constants are resolved by  
> > �self�.  
> > So, in short:
> > 
> > &nbsp;&nbsp;&nbsp;class A  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module B  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;A.new.instance\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > crashes in 1.8, but resolves B as A::B in 1.9. Some people hate that  
> > change  
> > in behaviour, I like it.
> > 
> > It doesn't directly answer your question, but I hope it gives you  
> > enough insight to find a way to do what you want.
> > 
> > Regards,  
> > Florian
> > 
> > On Oct 10, 2009, at 12:14 AM, Peter Pk wrote:
> > 
> > > \<br\>  
> > > instance of a class that defines the constant or subclass of it or  
> > > "const\_missing"
> > > 
> > > This so it can be like Java to get "this.CONST"  
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > --  
> > Florian Gilcher
> > 
> > smtp: flo@andersground.net  
> > jabber: Skade@jabber.ccc.de  
> > gpg: 533148E2
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Paul\_Harrington1](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Paul\_Harrington1](https://rubytalk.org/u/Paul_Harrington1)\
**Post date:** [10 October 2009 14:00 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/4 "2009-10-10T14:00:09Z")

</div>

well in Ruby 1.8 something like this works (obviously not it all  
cases, but you get the gist and can fix it as needed):

Obj.new { puts "CONST = #{self.class.const\_get("CONST")}" }

And what do you mean by "waiting" until Ruby 1.9? 1.9.1's been  
released and stable for the better part of this year now.

> **···**
>
> On Oct 10, 8:48 am, Peter Pk \<pet...@livingwork.com\> wrote:
> 
> > Thanks - actually it does answer the question, but it also answers that  
> > I have to wait until Ruby 1.9 for the "fix"
> > 
> > that is unless there is a workaround way (until 1.9) to
> > 
> > doInConstanScopeOf [Module] do  
> > # so instead of 'Module::CONST' one can do 'CONST'  
> > end
> > 
> > so one could do
> > 
> > def doIt(&block)  
> > doInConstanScopeOf AModule &block  
> > end
> > 
> > and explicitly define the constant scope to be the scope of "self.class"
> > 
> > so when will the fabled 1.9 be available 🙂
> > 
> > Florian Gilcher wrote:  
> > \> This is a bit of a strange problem. Ruby 1.8 resolves constants  
> > \> lexically.
> > 
> > \> So you are actually referring to ::CONST there.
> > 
> > \> In Ruby 1.9, this is different. In Ruby 1.9, Constants are resolved by  
> > \> self .  
> > \> So, in short:
> > 
> > \> class A  
> > \> module B  
> > \> end  
> > \> end
> > 
> > \> A.new.instance\_eval do  
> > \> B  
> > \> end
> > 
> > \> crashes in 1.8, but resolves B as A::B in 1.9. Some people hate that  
> > \> change  
> > \> in behaviour, I like it.
> > 
> > \> It doesn't directly answer your question, but I hope it gives you  
> > \> enough insight to find a way to do what you want.
> > 
> > \> Regards,  
> > \> Florian
> > 
> > \> On Oct 10, 2009, at 12:14 AM, Peter Pk wrote:
> > 
> > \>\> \<br\>  
> > \>\> instance of a class that defines the constant or subclass of it or  
> > \>\> "const\_missing"
> > 
> > \>\> This so it can be like Java to get "this.CONST"  
> > \>\> --  
> > \>\> Posted viahttp://www.ruby-forum.com/.
> > 
> > \> --  
> > \> Florian Gilcher
> > 
> > \> smtp: f...@andersground.net  
> > \> jabber: Sk...@jabber.ccc.de  
> > \> gpg: 533148E2
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [10 October 2009 19:03 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/5 "2009-10-10T19:03:24Z")

</div>

I got 1.9 and am now running it 🙂  
thanks!

pharrington wrote:

> **···**
>
> > On Oct 10, 8:48�am, Peter Pk \<pet...@livingwork.com\> wrote:
> > 
> > > \> change  
> > > \>\> \<br\>  
> > > \> smtp: � f...@andersground.net  
> > > \> jabber: Sk...@jabber.ccc.de  
> > > \> gpg: � �533148E2
> > > 
> > > --  
> > > Posted viahttp://www.ruby-forum.com/.
> > 
> > well in Ruby 1.8 something like this works (obviously not it all  
> > cases, but you get the gist and can fix it as needed):
> > 
> > Obj.new { puts "CONST = #{self.class.const\_get("CONST")}" }
> > 
> > And what do you mean by "waiting" until Ruby 1.9? 1.9.1's been  
> > released and stable for the better part of this year now.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Peter\_Pk](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@Peter\_Pk](https://rubytalk.org/u/Peter_Pk)\
**Post date:** [11 October 2009 13:38 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/6 "2009-10-11T13:38:10Z")

</div>

pharrington wrote:

> And what do you mean by "waiting" until Ruby 1.9? 1.9.1's been  
> released and stable for the better part of this year now.

That there are convenient installers/packages/etc available and  
documented for the OS' and distros that our admin sets up our  
development machines for 🙂 It looks like one still has to compile from  
source in many cases (such as Fedora). Windows was easy as someone made  
an NSIS installer 🙂

BTW 1.9 does exactly what I want and that is to overide const\_missing on  
a specific class. The old behavior also still works if not overidden  
(as the static method on Object, but the "self" will be the self of the  
object that was missing the constant. IMHO way better behavior to allow  
object oriented overides.

One can also on a specific class do:

alias :const\_missing :\_\_send\_\_

which will (efficiently) if a const is missing direct it to an attribute  
of the same name so you don't have to do thing = XXX() and can access it  
by thing = XXX

> **···**
>
> > On Oct 10, 8:48�am, Peter Pk \<pet...@livingwork.com\> wrote:
> 
> > > \> change  
> > > \>\> \<br\>  
> > > \> smtp: � f...@andersground.net  
> > > \> jabber: Sk...@jabber.ccc.de  
> > > \> gpg: � �533148E2
> > > 
> > > --  
> > > Posted viahttp://www.ruby-forum.com/.
> > 
> > well in Ruby 1.8 something like this works (obviously not it all  
> > cases, but you get the gist and can fix it as needed):
> > 
> > Obj.new { puts "CONST = #{self.class.const\_get("CONST")}" }
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [13 October 2009 08:52 UTC](https://rubytalk.org/t/scope-of-constants-in-instance-eval/55742/7 "2009-10-13T08:52:56Z")

</div>

pharrington wrote:

> well in Ruby 1.8 something like this works (obviously not it all  
> cases, but you get the gist and can fix it as needed):
> 
> Obj.new { puts "CONST = #{self.class.const\_get("CONST")}" }

Or more simply, self.class::CONST

I find this particularly useful when I wish to override a constant in a  
subclass, e.g.

class Foo  
&nbsp;&nbsp;TIMEOUT = 10  
&nbsp;&nbsp;def run  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Timeout is #{self.class::TIMEOUT}"  
&nbsp;&nbsp;end  
end

class Bar \< Foo  
&nbsp;&nbsp;TIMEOUT = 20  
end

Bar.new.run

Note that you need this trick even in ruby 1.9.2. Without it, the  
constant is still resolved statically, depending on in which class the  
method was defined:

$ irb19 --simple-prompt

> > RUBY\_VERSION

=\> "1.9.2"

> > RUBY\_DESCRIPTION

=\> "ruby 1.9.2dev (2009-07-18 trunk 24186) [i686-linux]"

> > class Foo  
> > &nbsp;&nbsp;TIMEOUT = 10  
> > &nbsp;&nbsp;def run  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Timeout is #{TIMEOUT}"  
> > &nbsp;&nbsp;end  
> > end

=\> nil

> > class Bar \< Foo  
> > &nbsp;&nbsp;TIMEOUT = 20  
> > end

=\> 20

> > Bar.new.run

Timeout is 10  
=\> nil

> > class Bar  
> > &nbsp;&nbsp;def run2  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Timeout is #{TIMEOUT}"  
> > &nbsp;&nbsp;end  
> > end

=\> nil

> > Bar.new.run2

Timeout is 20 ## ruby1.8 shows 10 here  
=\> nil

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
