# Block binding does not contains local variable

**URL:** <https://www.ruby-forum.com/t/block-binding-does-not-contains-local-variable/135143>\
**Category:** Ruby\
**Created:** [April 18, 2008, 11:54am UTC](https://www.ruby-forum.com/t/block-binding-does-not-contains-local-variable/135143 "2008-04-18T11:54:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artem\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@Artem\_V](https://www.ruby-forum.com/u/Artem_V)\
**Post date:** [April 18, 2008, 11:54am UTC](https://www.ruby-forum.com/t/block-binding-does-not-contains-local-variable/135143/1 "2008-04-18T11:54:33Z")

</div>

Maybe it contains.  
But please, explain me why “block binding” does not print a, b, and c  
variables.

def vars(&block)  
b = block.call(55)  
puts "inner binding: ", eval(“local\_variables”, b)  
puts "block binding: ", eval(“local\_variables”, block.binding)  
end

vars {|a|  
b = 1  
c = 2  
binding  
}

My output:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]  
inner binding:  
c  
b  
a  
block binding:

---

<div class="post-metadata">

**Author:** ![Artem\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@Artem\_V](https://www.ruby-forum.com/u/Artem_V)\
**Post date:** [April 18, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/block-binding-does-not-contains-local-variable/135143/2 "2008-04-18T13:07:19Z")

</div>

Hi –

On Fri, 18 Apr 2008, Artem V. wrote:

> vars {|a|  
> b  
> a  
> block binding:

My understanding is that given a Proc, there’s no way that Ruby can  
know what local variables are created inside it without running it, so  
binding of the Proc itself is the binding of the local context where  
it’s created. It’s only when the block is run that the local variables  
inside the block get bound to anything, so binding inside the block is  
a different binding and includes those variables.

You can see this if you do:

x = 1

and then call vars. block.binding will then include x.

David

---

<div class="post-metadata">

**Author:** ![Artem\_V](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@Artem\_V](https://www.ruby-forum.com/u/Artem_V)\
**Post date:** [April 20, 2008, 9:33am UTC](https://www.ruby-forum.com/t/block-binding-does-not-contains-local-variable/135143/3 "2008-04-20T09:33:05Z")

</div>

Thank you!
