# Eval, bindings and scope

**URL:** <https://www.ruby-forum.com/t/eval-bindings-and-scope/107530>\
**Category:** Ruby\
**Created:** [June 22, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530 "2007-06-22T17:33:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 22, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530/1 "2007-06-22T17:33:24Z")

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 22, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530/2 "2007-06-22T22:20:35Z")

</div>

On Jun 22, 2007, at 11:33 AM, removed\_email\_address@domain.invalid 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

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.

`
a = nil
foo = lambda { a }
eval("a = 42")
foo.call # => 42
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 22, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530/3 "2007-06-22T23:57:09Z")

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

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 23, 2007, 2:23am UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530/4 "2007-06-23T02:23:55Z")

</div>

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

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

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

> 1. 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:

`
bar = lambda { |b| b = b }
b = 0
bar.call(42) # => 42
b # => 0
``
b = 0
bar = lambda { |b| b = b }
bar.call(42) # => 42
b # => 42
`

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:

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.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [June 23, 2007, 5:59am UTC](https://www.ruby-forum.com/t/eval-bindings-and-scope/107530/5 "2007-06-23T05:59:11Z")

</div>

On 6/22/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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@:1(irb)  
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 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  
from (irb):6

> 1. 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@:1(irb)  
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 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  
from (irb):9  
irb(main):009:0\> eval “b = a”, binding  
=\> :inside  
irb(main):010:0\> b  
=\> :inside

Hope this helps!
