# Reffering back to caller's binding

**URL:** <https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873>\
**Category:** Ruby\
**Created:** [April 18, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873 "2009-04-18T19:16:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 18, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/1 "2009-04-18T19:16:54Z")

</div>

Hello Rubyists,

I am trying to define a helper method which displays a ruby expression  
and then displays the results of that expression via puts. Initially I  
did this, which obviously did not work:

def peval( expression )  
puts expression  
puts eval( expression )  
puts “”  
end

of course when I ran it, the scope of the method does not include  
objects in the calling scope… so then I realized it had to do with  
bindings and I defined this:

def peval  
f,b = yield  
puts f  
puts eval(f,b)  
puts “”  
end

and this works, but, its really ugly:

peval { [“map.inspect”, binding()] }

so my question is, is there a way to obtain the caller’s binding so that  
I do not have to specify it in the call? Is this in Pickaxe and I spaced  
out on that section? I eally just want to be able to specify the  
expression (in quotes), get it to display, see the results, move on…

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 19, 2009, 3:10am UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/2 "2009-04-19T03:10:49Z")

</div>

On Sat, Apr 18, 2009 at 6:16 PM, Owein H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> end
> 
> and this works, but, its really ugly:
> 
> peval { [“map.inspect”, binding()] }
> 
> so my question is, is there a way to obtain the caller’s binding so that  
> I do not have to specify it in the call? Is this in Pickaxe and I spaced  
> out on that section? I eally just want to be able to specify the  
> expression (in quotes), get it to display, see the results, move on…

Explicitly specify the block parameter and use its #binding method to  
get the binding for where the block was defined, i.e.

def peval(&block)  
f,b = block.call  
puts f  
puts eval(f,block.binding)  
puts “”  
end

peval { [“var.inspect”] }

OUTPUT:  
var.inspect  
[1, 2, 3]

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 19, 2009, 3:12am UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/3 "2009-04-19T03:12:37Z")

</div>

On Sun, Apr 19, 2009 at 2:10 AM, Sean O’Halpin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > puts “”  
> > puts “”
> 
> peval { [“var.inspect”] }
> 
> OUTPUT:  
> var.inspect  
> [1, 2, 3]
> 
> Regards,  
> Sean

I leave it as an exercise for the reader to work out the definition of  
‘var’ 😉

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 19, 2009, 7:36am UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/4 "2009-04-19T07:36:29Z")

</div>

> peval { [“var.inspect”] }

Interesting solution. The solution would be even better if ruby had a  
Proc#to\_str method built in or if #to\_a returned a sexpr (à la lisp)  
that could be easily manipulated, printed, and evaluated.

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 19, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/5 "2009-04-19T12:41:52Z")

</div>

On Sun, Apr 19, 2009 at 6:36 AM, Leo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > peval { [“var.inspect”] }
> 
> Interesting solution. The solution would be even better if ruby had a  
> Proc#to\_str method built in or if #to\_a returned a sexpr (à la lisp)  
> that could be easily manipulated, printed, and evaluated.

I agree - it would be nice if this was built-it. However, you can get  
it with ParseTree:

require ‘ruby2ruby’  
require ‘parse\_tree\_extensions’  
def dump(&block)  
puts block.to\_ruby.gsub(/^proc\s\*{(.\*)}$/, ‘\1’).strip  
puts block.call  
end

# need to call to\_ruby on a method before the to\_ruby call will work on

blocks  
def **dummy**  
end  
method(: **dummy** ).to\_ruby

var = [1,2,3]  
dump { var.inspect }  
#\> var.inspect  
#\> [1, 2, 3]

# also, just to show you can do this too:

p proc { 1 + 1 }.to\_sexp  
#\> s(:iter, s(:call, nil, :proc, s(:arglist)), nil, s(:call, s(:lit,  
1), :+, s(:arglist, s(:lit, 1))))

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 20, 2009, 12:56am UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/6 "2009-04-20T00:56:42Z")

</div>

Fabulous! Thank you so much for the help!

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 19, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/7 "2009-04-19T22:29:31Z")

</div>

On Apr 19, 2009, at 03:41 , Sean O’Halpin wrote:

> I agree - it would be nice if this was built-it. However, you can get  
> it with ParseTree:
> 
> require ‘ruby2ruby’  
> require ‘parse\_tree\_extensions’  
> def dump(&block)  
> puts block.to\_ruby.gsub(/^proc\s\*{(.\*)}$/, ‘\1’).strip  
> puts block.call  
> end

except that PT is essentially dead because it can’t work on 1.0…

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 20, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/8 "2009-04-20T14:00:11Z")

</div>

On Apr 19, 2009, at 16:14 , Sean O’Halpin wrote:

> > except that PT is essentially dead because it can’t work on 1.[9]…
> 
> I’m in denial 😕

have fun with that. I hope it works well for you.

---

<div class="post-metadata">

**Author:** ![oweinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a5b964/32.png) [@oweinh](https://www.ruby-forum.com/u/oweinh)\
**Post date:** [April 20, 2009, 1:16am UTC](https://www.ruby-forum.com/t/reffering-back-to-callers-binding/165873/9 "2009-04-20T01:16:16Z")

</div>

On Sun, Apr 19, 2009 at 9:28 PM, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > puts block.call  
> > end
> 
> except that PT is essentially dead because it can’t work on 1.0…

I’m in denial 😕
