# Why can't I define vars in caller's binding using eval?

**URL:** <https://www.ruby-forum.com/t/why-cant-i-define-vars-in-callers-binding-using-eval/196689>\
**Category:** Rails\
**Created:** [May 22, 2008, 5:40am UTC](https://www.ruby-forum.com/t/why-cant-i-define-vars-in-callers-binding-using-eval/196689 "2008-05-22T05:40:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [May 22, 2008, 5:40am UTC](https://www.ruby-forum.com/t/why-cant-i-define-vars-in-callers-binding-using-eval/196689/1 "2008-05-22T05:40:30Z")

</div>

In other words, why can’t I do this?

def f(b)  
eval(“x = 10”, b)  
end

f(binding)  
puts “x = #{x}”

Is there any way to make that code work (besides obviously setting x  
to something before calling f).

Thanks.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [May 22, 2008, 11:21am UTC](https://www.ruby-forum.com/t/why-cant-i-define-vars-in-callers-binding-using-eval/196689/2 "2008-05-22T11:21:48Z")

</div>

On 22 May 2008, at 04:39, Christopher J. Bottaro wrote:

> In other words, why can’t I do this?
> 
> def f(b)  
> eval(“x = 10”, b)  
> end
> 
> f(binding)  
> puts “x = #{x}”

This seems relevant:  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/a954f8aaf698a0b9/e1c761b71b63b82e?#e1c761b71b63b82e](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/a954f8aaf698a0b9/e1c761b71b63b82e?#e1c761b71b63b82e)

Fred
