# Instance\_eval and constants lookup rules in ruby 1.9

**URL:** <https://rubytalk.org/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/52872>\
**Category:** ruby-talk\
**Created:** [15 April 2009 16:13 UTC](https://rubytalk.org/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/52872 "2009-04-15T16:13:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Mendler](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@Daniel\_Mendler](https://rubytalk.org/u/Daniel_Mendler)\
**Post date:** [15 April 2009 16:13 UTC](https://rubytalk.org/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/52872/1 "2009-04-15T16:13:58Z")

</div>

Hi,

what happend to the constant lookup rules in ruby 1.9? It seems  
constants in blocks that are evaluated with instance\_eval are looked up  
only in the evaluating class but not in the scope the block was defined.

This works in Ruby 1.8 but not in 1.9:

> **···**
>
> ======================================
> 
> class X  
> &nbsp;&nbsp;def run(&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;instance\_eval(&block)  
> &nbsp;&nbsp;end  
> end
> 
> module A  
> &nbsp;&nbsp;I\_AM\_NOT\_FOUND = 666  
> &nbsp;&nbsp;a = X.new  
> &nbsp;&nbsp;a.run {  
> &nbsp;&nbsp;&nbsp;&nbsp;puts I\_AM\_NOT\_FOUND  
> &nbsp;&nbsp;}  
> end
> 
> Ruby 1.8 finds the constants only if they are in the scope where the  
> block is defined. Why was this behaviour changed?
> 
> Daniel

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [16 April 2009 03:57 UTC](https://rubytalk.org/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/52872/2 "2009-04-16T03:57:35Z")

</div>

i should expect that behaviour if using \*instance\*\_eval inside \*modules\*.

i have many options though (note test2.rb does not use instance\_eval)...

botp@jedi-hopeful:~$ cat test?.rb

#--test1.rb------------  
class X  
def run(&block)  
&nbsp;&nbsp;&nbsp;instance\_eval(&block)  
end  
end  
module A  
I\_AM\_FOUND = 666  
a = X.new  
a.run {  
&nbsp;&nbsp;&nbsp;puts A::I\_AM\_FOUND  
}  
end

#-- test2.rb ---------  
class X  
def run(&block)  
&nbsp;&nbsp;&nbsp;block.call  
end  
end  
module A  
I\_AM\_FOUND = 666  
a = X.new  
a.run {  
&nbsp;&nbsp;&nbsp;puts I\_AM\_FOUND  
}  
end

#-- test3.rb -----------  
class X  
&nbsp;&nbsp;I\_AM\_FOUND = 666  
&nbsp;&nbsp;def run(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval(&block)  
&nbsp;&nbsp;end  
end  
module A  
a = X.new  
a.run {  
&nbsp;&nbsp;&nbsp;puts I\_AM\_FOUND  
}  
end

#-- test4.rb -----------  
class X  
def run(&block)  
&nbsp;&nbsp;&nbsp;instance\_eval(&block)  
end  
end  
I\_AM\_FOUND = 666  
a = X.new  
a.run {  
&nbsp;&nbsp;puts I\_AM\_FOUND  
}

botp@jedi-hopeful:~$

kind regards -botp

> **···**
>
> On Thu, Apr 16, 2009 at 12:13 AM, Daniel Mendler \<[dmendler@wurzelteiler.de](mailto:dmendler@wurzelteiler.de)\> wrote:
