# Constants, Bindings and instance\_eval

**URL:** <https://www.ruby-forum.com/t/constants-bindings-and-instance-eval/137376>\
**Category:** Ruby\
**Created:** [May 12, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/constants-bindings-and-instance-eval/137376 "2008-05-12T16:05:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![achim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@achim](https://www.ruby-forum.com/u/achim)\
**Post date:** [May 12, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/constants-bindings-and-instance-eval/137376/1 "2008-05-12T16:05:11Z")

</div>

Dear rubyists,

i’m fairly new to ruby, so please apologize if i’m missing something  
obvious. I’m trying to figure out how constant lookup works and find  
myself confused. Here we go:

```
class A; C=3 end

$a = A.new
$a.instance_eval "C" # => 3
$a.instance_eval {C}
    # ~> NameError: uninitialized constant C [...]

```

This is ruby 1.8.6. My naive mental model of what’s happening: constants  
are evaluated statically before the block is passed along, and wrapping  
it up as a string prevents the interpreter from looking inside too  
early. 🙂  
That seems to have changed with Ruby 1.9 (yesterday’s trunk), but there  
are a few surprises still:

```
def a_eval(&block) $a.instance_eval(&block) end

$a.instance_eval {C} # => 3
a_eval {C}
    # ~> NameError: uninitialized constant C [...]
a_eval {instance_eval {C}} # => 3 -- ???

```

I gather it’s a binding issue, but why should the binding of constants  
(of anything, really) differ between inside “self.instance\_eval { … }”  
and outside?

```
a_eval {binding}.eval("C")
    # ~> NameError: uninitialized constant C [...]
a_eval {instance_eval{binding}}.eval("C") # => 3
$a.instance_eval {binding}.eval("C") # => 3

```

Can anybody shed some light on this? Thanks a lot!

Kind regards,  
achim

---

<div class="post-metadata">

**Author:** ![achim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@achim](https://www.ruby-forum.com/u/achim)\
**Post date:** [May 12, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/constants-bindings-and-instance-eval/137376/2 "2008-05-12T21:48:21Z")

</div>

Achim Passen wrote:

> Dear rubyists,
> 
> i’m fairly new to ruby, so please apologize if i’m missing something  
> obvious. I’m trying to figure out how constant lookup works and find  
> myself confused. Here we go:
> 
> ```
> class A; C=3 end
> 
> $a = A.new
> $a.instance_eval "C" # => 3
> $a.instance_eval {C}
> # ~> NameError: uninitialized constant C [...]
> 
> ```
> 
> Can anybody shed some light on this? Thanks a lot!
> 
> Kind regards,  
> achim

class A  
C=3  
end

$a = A.new  
$a.instance\_eval “C”  
#=\> 3

$a.instance\_eval {C}

# ~\> NameError: uninitialized constant C […]

Its not being called properly for 1.8

instead use this…

$a.instance\_eval {A::C}

A being the class, and C being the constant.

Regards,

- Mac

---

<div class="post-metadata">

**Author:** ![achim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@achim](https://www.ruby-forum.com/u/achim)\
**Post date:** [May 12, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/constants-bindings-and-instance-eval/137376/3 "2008-05-12T22:13:19Z")

</div>

Achim Passen wrote:

> I’m trying to figure out how constant lookup works […]
> 
> class A; C=3 end
> 
> $a = A.new  
> $a.instance\_eval “C” # =\> 3  
> $a.instance\_eval {C}  
> # ~\> NameError: uninitialized constant C […]

That’s an interesting question!

Michael L. already gave an “answer”, but I think I have another  
one.

Look here:

class A  
C=3  
def prt\_C  
p C  
end  
end

a = A.new

def a.another\_prt\_C  
p C  
end

a.prt\_C # OK!  
a.another\_prt\_C # fails!

the ‘another\_prt\_C’ method fails to see the constant. I have the book  
‘The Ruby P.ming Language’ and it has a page about ‘Constant  
Lookup’. That page explains that “ruby attempts to resolve a constant  
reference in the _lexical_ scope of the reference. This means that it  
first checks the class or module that encloses the constant reference.”  
In other words, since our class A doesn’t enclose the ‘another\_prt\_C’  
method, this method can’t reference the constant (directly). “Lexical”  
means textual. That page also continues to explain that if the lookup  
fails, ruby checks the class hierarchy as well. But the wording of the  
book here is vague and there’s roon for interpretation that  
‘another\_prt\_C’ indeed shouldn’t work and that it’s not a bug.

That’s why you shouldn’t expect instance\_eval to see ‘C’: because this  
constant isn’t defined in a class or module _lexically enclosing_ your  
block.

You say it’s fixed in 1.9. It probably means that ‘another\_prt\_C’ too  
works in 1.9, or else my nice theory should be garbage collected! If it  
does work in 1.9, I guess what they fixed is the section I said was  
vague.
