[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: inspect...and modify a proc object?

w wg

1/17/2007 1:19:00 AM

myproc.methods

2007/1/17, littleears <felkinator-rt@yahoo.com>:
> I'm trying to understand proc objects.
>
> Can you inspect them? Is there anyway to walk through the original context? Is there anyway to identify the objects or classes involved, and what method is being invoked, with what parameters?
>
> Currently, when I "puts myproc.inspect", the result is not very useful...something like:
>
> #<Proc:0x02815180@...
>
> Thanks,
>
> -Kelly
>


--
--
WenGe Wang

4 Answers

Kelly Dwight Felkins

1/21/2007 7:40:00 PM

0

myproc.methods returns the methods of the proc object itself. The proc
object, I guess, is a container for the original block. I'm interested
in the contents of the original block.

Any suggestions for exposing details about the original block?

Thanks,

-Kelly


Gregory Brown

1/21/2007 7:54:00 PM

0

On 1/21/07, kelly <railsinator@gmail.com> wrote:
> myproc.methods returns the methods of the proc object itself. The proc
> object, I guess, is a container for the original block. I'm interested
> in the contents of the original block.
>
> Any suggestions for exposing details about the original block?

this seems just plain evil, so please think about why you'd ever do
it, and also, look for better solutions! :)

seltzer:~ sandal$ irb
>> class Proc
>> def eval_with_binding(string)
>> eval string, binding
>> end
>> end
=> nil
>> b = 20
=> 20
>> a = lambda { |x| x + b }
=> #<Proc:0x0032ce84@(irb):7>
>> a.eval_with_binding("local_variables")
=> ["_", "__", "b", "a"]
>> class B
>> def local_vars_for_proc(p)
>> p.eval_with_binding("local_variables")
>> end
>> end
=> nil
>> c = B.new
=> #<B:0x31b3dc>
>> c.local_vars_for_proc(a)
=> ["_", "__", "b", "a", "c"]

Gregory Brown

1/21/2007 8:01:00 PM

0

On 1/21/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> On 1/21/07, kelly <railsinator@gmail.com> wrote:
> > myproc.methods returns the methods of the proc object itself. The proc
> > object, I guess, is a container for the original block. I'm interested
> > in the contents of the original block.
> >
> > Any suggestions for exposing details about the original block?
>
> this seems just plain evil, so please think about why you'd ever do
> it, and also, look for better solutions! :)

Actually, sorry that doesn't do the trick. It'll give you the
enclosing scope, but not access to the block local scope.

>> b = lambda { |x| n=3; x + a }
=> #<Proc:0x0031bdc8@(irb):10>
>> b.eval_with_binding('n')
NameError: undefined local variable or method `n' for main:Object
from (irb):10
from (irb):10
>> b[10]
=> 20
>> b.eval_with_binding('n')
NameError: undefined local variable or method `n' for main:Object
from (irb):10
from (irb):10

Kelly Dwight Felkins

1/31/2007 10:14:00 PM

0

> On 1/21/07, Gregory Brown <gregory.t.br...@gmail.com> wrote:
> > > Any suggestions for exposing details about the original block?

> > this seems just plain evil, so please think about why you'd ever do
> > it, and also, look for better solutions! :)

I think Ruby tries not to be judgmental about such things -- it is an
open and trusting language:

- Want to add features to an existing class? Please be my guest...
- Want to change private values in an object? By all means...
- Want to change the value of a constant? Why not?

Please trust that I'm not evil.....

MUUU HHHHHAAAAAA HA Ha ha ha ha