# Create local variable in Binding after it's creation

**URL:** <https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702>\
**Category:** Ruby\
**Created:** [November 12, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702 "2006-11-12T15:07:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/1 "2006-11-12T15:07:54Z")

</div>

Hi,

Is it possible to create a new local variable in a binding after it’s  
creation?

Specifically, what I’d like to do:

l = lambda { puts a } # ‘a’ not defined at this point.  
eval(‘a = 7’, l.binding) # Hope to create a in l’s binding  
l.call # Hope to get ‘7’

- but that produces:

Apparently, ‘a’ gets created in the scope from where ‘eval’ was called  
and not in ‘l’’'s binding.

Any suggestions would be most welcome.

best regards  
Christian S.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/2 "2006-11-12T15:19:55Z")

</div>

On Sun, Nov 12, 2006 at 11:06:44PM +0900, Christian S. wrote:

> Is it possible to create a new local variable in a binding after it’s creation?

You can create it, but you won’t be able to use it directly.

> Specifically, what I’d like to do:
> 
> l = lambda { puts a } # ‘a’ not defined at this point.  
> eval(‘a = 7’, l.binding) # Hope to create a in l’s binding  
> l.call # Hope to get ‘7’

l = lambda{ eval ‘a’ }  
eval(‘a = 7’, l.binding)  
l.call # =\> 7  
eval(‘a’) # =\> 7

If you try to use ‘a’ before the parser sees ‘a = …’, it will be  
considered  
a method call, and you’ll get a NameError even if you give it a value  
afterwards/in eval.

BTW. this is changing in 1.9.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/3 "2006-11-12T15:28:01Z")

</div>

Søndag 12 november 2006 15:18 skrev Mauricio F.:

> If you try to use ‘a’ before the parser sees ‘a = …’, it will be considered  
> a method call, and you’ll get a NameError even if you give it a value  
> afterwards/in eval.

Ah, I see. Thanks.

> BTW. this is changing in 1.9.

In what way? My example seems to work the same way with ruby-1.9…

best regards

Christian S.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/4 "2006-11-12T16:24:08Z")

</div>

On 11/12/06, Mauricio F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > eval(‘a = 7’, l.binding) # Hope to create a in l’s binding  
> > afterwards/in eval.
> 
> BTW. this is changing in 1.9.
> 
> –  
> Mauricio F. - [http://eigenclass.org](http://eigenclass.org) - singular Ruby
> 
> I do not want to argue with Mauricio, I know when I am outclassed(1), but  
> maybe one should suggest a different approach to OP.  
> Can you use instant variables, or will that be impacting elsewhere

p = Proc.new { puts @a }  
@a= Guess what?  
p.call

And yes Mauricio can you give a pointer to the new behavior please, thx.

Cheers  
Robert

(1) Well I do not, but that remains between us 😉

–  
The reasonable man adapts himself to the world; the unreasonable one  
persists in trying to adapt the world to himself. Therefore all progress  
depends on the unreasonable man.

- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/5 "2006-11-12T16:28:09Z")

</div>

On Sun, Nov 12, 2006 at 11:27:07PM +0900, Christian S. wrote:

> In what way? My example seems to work the same way with ruby-1.9…

Both  
eval(“a = 7”)  
eval(“a”)  
and  
eval(“a = 7”, binding)  
eval(“a”)  
will raise a NameError in the future, so you’ll have to do  
binding = binding # heh  
eval(“a = 7”, binding)  
eval(“a”, binding)

[ruby-core:09388]

This can be seen as a step towards less inclusive closures and lambda  
lifting.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [November 12, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/create-local-variable-in-binding-after-its-creation/75702/6 "2006-11-12T17:04:13Z")

</div>

On Mon, Nov 13, 2006 at 12:22:26AM +0900, Robert D. wrote:

> > If you try to use ‘a’ before the parser sees ‘a = …’, it will be  
> > considered a method call, and you’ll get a NameError even if you give it a  
> > value afterwards/in eval.
> > 
> > BTW. this is changing in 1.9.

(Just to be clear, “this” == semantics of eval(“local\_var”)).

> And yes Mauricio can you give a pointer to the new behavior please, thx.

[ruby-core:09388]

I’ll be glad to see that happen; it’s something I’ve been wanting for  
some  
time. I became concerned about closures being “too wide” by the end of  
2003/early 2004 and was close to writing a RCR.  
([http://thekode.net/misc/survey.txt](http://thekode.net/misc/survey.txt) — note: I’m not collecting  
responses  
anymore 🙂  
[ruby-core:09388] can be interpreted as a step in that direction.

PS: your email made me think of the zero derivation of eigenclass and  
“verbing” it in sentences like  
“I want to eigenclass myself” or  
“eigenclassing oneself demands constant effort”  
which could be the first use ever with the obvious newly-coined?  
acception 🙂
