# Nuby with instance\_eval question

**URL:** <https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545>\
**Category:** Ruby\
**Created:** [August 26, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545 "2006-08-26T20:44:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [August 26, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545/1 "2006-08-26T20:44:52Z")

</div>

Wondering why this works the way it does:

```
  1 class Klass
  2
  3 attr_accessor :attr1
  4
  5 def initialize (&block)
  6 chum = "chum_in_Klass"
  7 instance_eval(&block) # Why does instance_eval pick up 

```

self.attr1 from Klass but not ‘chum’??  
8 eval\_attr1  
9 end  
10 def eval\_attr1  
11 @ok = self.attr1.call  
12 end  
13 end  
14  
15 chum = “chum\_in\_main”  
16  
17 foo=Klass.new {  
18 self.attr1 = proc { chum }  
19 }  
20  
21 puts foo.inspect # -\> foo.ok = “chum\_in\_main”  
22  
23 chum=“chum\_in\_main\_again”  
24 foo.eval\_attr1  
25  
26 puts foo.inspect # -\> foo.ok = “chum\_in\_main\_again”

I have a constructor that uses a block to intialize the object’s  
members. Nested on the RHS of a member assignment is a proc block. The  
proc block carries the value of chum from the “main” namespace, whereas  
I expected it would take the chum from the Klass. I figured the proc  
would not be evaluated and bound to a namespace until the passed block  
was instance\_eval’d in the constructpr, and thereby pick up a binding  
from Klass… This makes my head hurt!

The observed behaviour is what I want but I didn’t think it was  
possible. I want to make sure I’m not taking advantage of a “bug” or a  
language ambgiuity. If someone could make my eval/proc light bulb come  
on it would be much appreciated…

Thanks

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [August 27, 2006, 7:21am UTC](https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545/2 "2006-08-27T07:21:13Z")

</div>

> self.attr1 from Klass but not ‘chum’??  
> 18 self.attr1 = proc { chum }  
> members. Nested on the RHS of a member assignment is a proc  
> or a  
> language ambgiuity. If someone could make my eval/proc light bulb  
> come  
> on it would be much appreciated…

chum is a local variable in both places. instance\_eval only affects  
self and class variables, not local variables.

Matthew

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [August 27, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545/3 "2006-08-27T20:02:53Z")

</div>

OK, thanks for the responses. Let’s see if I got this right :

Blocks passed to instance\_eval are still bound to where ever they were  
declared but instance\_eval will use instance & class variables where  
ever required.

---

<div class="post-metadata">

**Author:** ![glub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e480ec/32.png) [@glub](https://www.ruby-forum.com/u/glub)\
**Post date:** [August 27, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/nuby-with-instance-eval-question/66545/4 "2006-08-27T18:18:00Z")

</div>

Geoff Barnes wrote:

> Wondering why this works the way it does:
> 
> ```
> 1 class Klass
> 2
> 3 attr_accessor :attr1
> 4
> 5 def initialize (&block)
> 6 chum = "chum_in_Klass"
> 7 instance_eval(&block) # Why does instance_eval pick up 
> 
> ```
> 
> self.attr1 from Klass but not ‘chum’??  
> 8 eval\_attr1  
> 9 end  
> 10 def eval\_attr1  
> 11 @ok = self.attr1.call  
> 12 end  
> 13 end  
> 14  
> 15 chum = “chum\_in\_main”  
> 16  
> 17 foo=Klass.new {  
> 18 self.attr1 = proc { chum }  
> 19 }

You are creating a closure here and, since ‘chum’ exists in  
this scope (you would get an error otherwise), it is bound  
here.

> ```
> 20
> 21 puts foo.inspect # -> foo.ok = "chum_in_main"
> 22
> 23 chum="chum_in_main_again"
> 24 foo.eval_attr1
> 25
> 26 puts foo.inspect # -> foo.ok = "chum_in_main_again"
> 
> ```
> 
> Thanks
