# Eval, bindings and scope

**URL:** <https://rubytalk.org/t/eval-bindings-and-scope/38546>\
**Category:** ruby-talk\
**Created:** [22 June 2007 15:33 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546 "2007-06-22T15:33:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hell\_Feuer](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Hell\_Feuer](https://rubytalk.org/u/Hell_Feuer)\
**Post date:** [22 June 2007 15:33 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546/1 "2007-06-22T15:33:46Z")

</div>

hey

i have a question:  
if i do this in the top level:

> foo = lambda {p a}  
> eval "a=1"  
> foo.call

It doesn't work, giving me a NameError: undefined variable or method in  
main:Object

but whats perhaps wierder, is if i do this:

> foo = lambda {p b}  
> eval "b=1", foo  
> foo.call

it gives the same error, but

> p b

now works. that is, b is bound in the global scope

now my understanding is:  
1) the first version should bind 'a' in the global scope (it does), and  
foo should now be able to access it (it cant)  
2) the second version should bind 'b' in the context of foo's binding  
therefore making it local to foo, so foo should still be able to access  
b, but it should not be visible in the top level.. yet the opposite  
behaviour is observed

how come?? what am i missing here?

thnx

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

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [22 June 2007 20:19 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546/2 "2007-06-22T20:19:27Z")

</div>

It will work if the local variable has been defined before the Proc object is created. That is, the local variable must exist before the closure is made.

\<code\>  
a = nil  
foo = lambda { a }  
eval("a = 42")  
foo.call # =\> 42  
\</code\>

Regards, Morton

> **···**
>
> On Jun 22, 2007, at 11:33 AM, hellfeuer@gmail.com wrote:
> 
> > if i do this in the top level:
> > 
> > > foo = lambda {p a}  
> > > eval "a=1"  
> > > foo.call
> > 
> > It doesn't work, giving me a NameError: undefined variable or method in  
> > main:Object

---

<div class="post-metadata">

**Author:** ![Sami\_Samhuri](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@Sami\_Samhuri](https://rubytalk.org/u/Sami_Samhuri)\
**Post date:** [23 June 2007 03:58 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546/3 "2007-06-23T03:58:09Z")

</div>

> hey

Hi!

> i have a question:

A nice one too. I learned quite a bit from it so thank you.

After reading up on procs, evals, and lambdas in the pickaxe book I  
think I understand your question. This irb snippet is where I realised  
exactly what was going on so maybe it will help you as well.

sjs@tuono% ruby -v  
ruby 1.8.6 (2007-03-13 patchlevel 0) [x86\_64-linux]  
sjs@tuono% irb  
irb(main):001:0\> p = lambda {a=:a; binding}  
=\> #\<Proc:0x00002b17e252f1a0@(irb):1\>  
irb(main):002:0\> binding = p.call  
=\> #\<Binding:0x2b17e252a2b8\>  
irb(main):003:0\> eval "p a", binding  
:a  
=\> nil  
irb(main):004:0\> p a  
NameError: undefined local variable or method `a' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
irb(main):005:0\> eval "b=:b", binding  
=\> :b  
irb(main):006:0\> b  
NameError: undefined local variable or method `b' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6

> 2) the second version should bind 'b' in the context of foo's binding  
> therefore making it local to foo, so foo should still be able to access  
> b, but it should not be visible in the top level.. yet the opposite  
> behaviour is observed
> 
> how come?? what am i missing here?

This is how you you would define b in a given context, local to that  
context. I don't think you were misunderstanding the expected  
behaviour, just the method of describing that behaviour.

sjs@tuono% irb  
irb(main):001:0\> p = lambda {a=:inside; binding}  
=\> #\<Proc:0x00002b321f1aee48@(irb):1\>  
irb(main):002:0\> binding = p.call  
=\> #\<Binding:0x2b321f1a9c40\>  
irb(main):003:0\> p a # this is the same as: eval "p a"  
NameError: undefined local variable or method `a' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3  
irb(main):004:0\> eval "p a", binding  
:inside  
=\> nil  
irb(main):005:0\> a = :outside  
=\> :outside  
irb(main):006:0\> b = a  
=\> :outside  
irb(main):007:0\> eval "c = a", binding  
=\> :inside  
irb(main):008:0\> c  
NameError: undefined local variable or method `c' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):9  
irb(main):009:0\> eval "b = a", binding  
=\> :inside  
irb(main):010:0\> b  
=\> :inside

Hope this helps!

> **···**
>
> On 6/22/07, hellfeuer@gmail.com \<hellfeuer@gmail.com\> wrote:
> 
> --  
> Sami Samhuri

---

<div class="post-metadata">

**Author:** ![Hell\_Feuer](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Hell\_Feuer](https://rubytalk.org/u/Hell_Feuer)\
**Post date:** [22 June 2007 21:57 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546/4 "2007-06-22T21:57:22Z")

</div>

hmmm.. thnx. ur right. but even accepting that thats just how closures  
are, what about my second version?

> foo = lambda {p b}  
> eval "b=1", foo  
> foo.call

1) why does b get bound in the global scope??? shouldn't it become local  
to to foo?

2) and in any case, since b=1 is being evaluated in the context of foo's  
binding, and foo is being called later, shouldn't this be equivalent to  
defining b before foo is called (and therefore this shouldn't give an  
error)??

3) if not, what does passing foo to eval actually do, since the  
behaviour seems to be the same whether or not i pass foo (i.e. the  
variable is bound in the global scope, and the closure cannot see it)

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

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [23 June 2007 00:22 UTC](https://rubytalk.org/t/eval-bindings-and-scope/38546/5 "2007-06-23T00:22:56Z")

</div>

> hmmm.. thnx. ur right. but even accepting that thats just how closures  
> are, what about my second version?
> 
> > > foo = lambda {p b}  
> > > eval "b=1", foo  
> > > foo.call
> 
> 1) why does b get bound in the global scope???

There is nothing in your lambda to establish a binding for b.

> shouldn't it become local to to foo?

No.

> 2) and in any case, since b=1 is being evaluated in the context of foo's  
> binding, and foo is being called later, shouldn't this be equivalent to  
> defining b before foo is called (and therefore this shouldn't give an  
> error)??

Only assignment brings b into existence.

> 3) if not, what does passing foo to eval actually do, since the  
> behaviour seems to be the same whether or not i pass foo (i.e. the  
> variable is bound in the global scope, and the closure cannot see it)

The binding of the Proc object is passed to eval.

Your second example suffers from the same problem as your first.

Also, consider the following tow examples:

\<code\>  
bar = lambda { |b| b = b }  
b = 0  
bar.call(42) # =\> 42  
b # =\> 0  
\</code\>

\<code\>  
b = 0  
bar = lambda { |b| b = b }  
bar.call(42) # =\> 42  
b # =\> 42  
\</code\>

In the first case, b is local to the block because no previous definition is visible, but in the second b is not local to the block because a previous definition is visible.

Finally, here is a quote from the pickaxe book that might clarify things for you:

\<quote\>  
As of Ruby 1.8,local variables assigned within an eval are available after the eval  
only if they were defined at the outer scope before the eval executed. In this way eval  
has the same scoping rules as blocks.  
\</quote\>

Regards, Morton

> **···**
>
> On Jun 22, 2007, at 5:57 PM, Hell Feuer wrote:
