# Nuby with instance\_eval question

**URL:** <https://rubytalk.org/t/nuby-with-instance-eval-question/30367>\
**Category:** ruby-talk\
**Created:** [27 August 2006 03:14 UTC](https://rubytalk.org/t/nuby-with-instance-eval-question/30367 "2006-08-27T03:14:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geoff\_Barnes](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Geoff\_Barnes](https://rubytalk.org/u/Geoff_Barnes)\
**Post date:** [27 August 2006 03:14 UTC](https://rubytalk.org/t/nuby-with-instance-eval-question/30367/1 "2006-08-27T03:14:53Z")

</div>

Wondering why this works the way it does:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 class Klass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 attr\_accessor :attr1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 def initialize (&block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 chum = "chum\_in\_Klass"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 instance\_eval(&block) # Why does instance\_eval pick up  
self.attr1 from Klass but not 'chum'??  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 eval\_attr1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 def eval\_attr1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11 @ok = self.attr1.call  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13 end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 chum = "chum\_in\_main"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;17 foo=Klass.new {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18 self.attr1 = proc { chum }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;19 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21 puts foo.inspect # -\> foo.ok = "chum\_in\_main"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;22  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23 chum="chum\_in\_main\_again"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24 foo.eval\_attr1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

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

---

<div class="post-metadata">

**Author:** ![Matthew\_Johnson](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@Matthew\_Johnson](https://rubytalk.org/u/Matthew_Johnson)\
**Post date:** [27 August 2006 05:19 UTC](https://rubytalk.org/t/nuby-with-instance-eval-question/30367/2 "2006-08-27T05:19:59Z")

</div>

> Wondering why this works the way it does:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 class Klass  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 attr\_accessor :attr1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 def initialize (&block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 chum = "chum\_in\_Klass"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 instance\_eval(&block) # Why does instance\_eval pick up  
> self.attr1 from Klass but not 'chum'??  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8 eval\_attr1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;9 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10 def eval\_attr1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11 @ok = self.attr1.call  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;13 end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 chum = "chum\_in\_main"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;17 foo=Klass.new {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;18 self.attr1 = proc { chum }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;19 }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21 puts foo.inspect # -\> foo.ok = "chum\_in\_main"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;22  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23 chum="chum\_in\_main\_again"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24 foo.eval\_attr1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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..

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:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [27 August 2006 16:18 UTC](https://rubytalk.org/t/nuby-with-instance-eval-question/30367/3 "2006-08-27T16:18:03Z")

</div>

Geoff Barnes wrote:

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

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

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;21 puts foo.inspect # -\> foo.ok = "chum\_in\_main"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;22  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;23 chum="chum\_in\_main\_again"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;24 foo.eval\_attr1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26 puts foo.inspect # -\> foo.ok = "chum\_in\_main\_again"
> > 
> > \<snip /\>
> > 
> > Thanks
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Geoff\_Barnes](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Geoff\_Barnes](https://rubytalk.org/u/Geoff_Barnes)\
**Post date:** [27 August 2006 18:03 UTC](https://rubytalk.org/t/nuby-with-instance-eval-question/30367/4 "2006-08-27T18:03:00Z")

</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.

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