# Any way to evaluate blocks using an arbitrary Binding?

**URL:** <https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432>\
**Category:** Ruby\
**Created:** [August 31, 2007, 1:00am UTC](https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432 "2007-08-31T01:00:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 1:00am UTC](https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432/1 "2007-08-31T01:00:05Z")

</div>

Using Kernel.eval, I can evaluate a Ruby string under an arbitrary  
variable Binding object. However, while I can obtain from a Proc the  
Binding it uses when it is evaluated, there does not seem to be any way  
of evaluating a Proc with a different Binding. Is this possible in some  
way?

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 8:07am UTC](https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432/2 "2007-08-31T08:07:34Z")

</div>

On 31.08.2007 00:58, Kenneth McDonald wrote:

> Using Kernel.eval, I can evaluate a Ruby string under an arbitrary  
> variable Binding object. However, while I can obtain from a Proc the  
> Binding it uses when it is evaluated, there does not seem to be any way  
> of evaluating a Proc with a different Binding. Is this possible in some  
> way?

Sort of: you can use instance\_eval and class\_eval with a block. While  
only “self” will be rebound this is sufficient in many cases.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 8:57am UTC](https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432/3 "2007-08-31T08:57:40Z")

</div>

Robert,

I was already aware of instance\_eval (have to look up class\_eval) used  
in this manner (possibly from another of your very useful postings), but  
had wondered if there was a more general way of affecting the execution  
environment. Oh well.

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [August 31, 2007, 9:08am UTC](https://www.ruby-forum.com/t/any-way-to-evaluate-blocks-using-an-arbitrary-binding/113432/4 "2007-08-31T09:08:42Z")

</div>

Kenneth McDonald wrote:

> Robert,
> 
> I was already aware of instance\_eval (have to look up class\_eval) used  
> in this manner (possibly from another of your very useful postings), but  
> had wondered if there was a more general way of affecting the execution  
> environment. Oh well.

You can change the binding one variable at a time:

def foo  
x = 1  
proc {puts “x = #{x}”}  
end

pr = foo

p eval(“x”, pr)  
eval(“x=2”, pr)  
p eval(“x”, pr)

pr.call

**END**

Output:

1  
2  
x = 2
