# Instance\_eval and constants lookup rules in ruby 1.9

**URL:** <https://www.ruby-forum.com/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/165607>\
**Category:** Ruby\
**Created:** [April 15, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/165607 "2009-04-15T18:15:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [April 15, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/165607/1 "2009-04-15T18:15:01Z")

</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  
def run(&block)  
instance\_eval(&block)  
end  
end

module A  
I\_AM\_NOT\_FOUND = 666  
a = X.new  
a.run {  
puts I\_AM\_NOT\_FOUND  
}  
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:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [April 16, 2009, 5:58am UTC](https://www.ruby-forum.com/t/instance-eval-and-constants-lookup-rules-in-ruby-1-9/165607/2 "2009-04-16T05:58:20Z")

</div>

On Thu, Apr 16, 2009 at 12:13 AM, Daniel M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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)  
instance\_eval(&block)  
end  
end  
module A  
I\_AM\_FOUND = 666  
a = X.new  
a.run {  
puts A::I\_AM\_FOUND  
}  
end

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

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

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

botp@jedi-hopeful:~$

kind regards -botp
