[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reffering back to caller's binding

Owein Herrmann

4/18/2009 5:17:00 PM

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...
--
Posted via http://www.ruby-....

6 Answers

Sean O'Halpin

4/19/2009 1:10:00 AM

0

On Sat, Apr 18, 2009 at 6:16 PM, Owein Herrmann <oherrmann@gmail.com> wrote=
:
> 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 )
> =A0puts expression
> =A0puts eval( expression )
> =A0puts ""
> 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
> =A0f,b =3D yield
> =A0puts f
> =A0puts eval(f,b)
> =A0puts ""
> 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 =3D block.call
puts f
puts eval(f,block.binding)
puts ""
end

peval { ["var.inspect"] }

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

Regards,
Sean

Sean O'Halpin

4/19/2009 1:12:00 AM

0

On Sun, Apr 19, 2009 at 2:10 AM, Sean O'Halpin <sean.ohalpin@gmail.com> wro=
te:
> On Sat, Apr 18, 2009 at 6:16 PM, Owein Herrmann <oherrmann@gmail.com> wro=
te:
>> 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 )
>> =A0puts expression
>> =A0puts eval( expression )
>> =A0puts ""
>> 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
>> =A0f,b =3D yield
>> =A0puts f
>> =A0puts eval(f,b)
>> =A0puts ""
>> 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)
> =A0f,b =3D block.call
> =A0puts f
> =A0puts eval(f,block.binding)
> =A0puts ""
> end
>
> 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=
' ;)

Ryan Davis

4/19/2009 8:28:00 PM

0


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...


Owein Herrmann

4/19/2009 10:57:00 PM

0

Fabulous! Thank you so much for the help!
--
Posted via http://www.ruby-....

Sean O'Halpin

4/19/2009 11:15:00 PM

0

On Sun, Apr 19, 2009 at 9:28 PM, Ryan Davis <ryand-ruby@zenspider.com> wrot=
e:
>
> 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)
>> =A0puts block.to_ruby.gsub(/^proc\s*\{(.*)\}$/, '\1').strip
>> =A0puts block.call
>> end
>
> except that PT is essentially dead because it can't work on 1.0...

I'm in denial :/

Ryan Davis

4/20/2009 12:00:00 PM

0


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.