[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Proc code

Marc Soda

1/9/2007 3:36:00 AM

Hey all,

Is there a way to get the code (or, more importantly, the variables)
out of a block associated with a Proc? For example:

foo = lamdba { a = 'bar'; puts a }
p foo.source

produces "a = 'bar'; puts a"

I thought about extending Proc and saving the block in initialize, but
this seems like a lot of overhead if you have a lot of Procs.

Any better ideas?

Thanks,
Marc

4 Answers

Trans

1/9/2007 4:36:00 AM

0


Marc Soda wrote:
> Hey all,
>
> Is there a way to get the code (or, more importantly, the variables)
> out of a block associated with a Proc? For example:
>
> foo = lamdba { a = 'bar'; puts a }
> p foo.source
>
> produces "a = 'bar'; puts a"
>
> I thought about extending Proc and saving the block in initialize, but
> this seems like a lot of overhead if you have a lot of Procs.
>
> Any better ideas?

Not the source, but you can use local_varaibles against the binding of
the proc. Check out Facets' binding methods too which can make it
easier.

http://facets.rub...

T.


Giles Bowkett

1/9/2007 8:36:00 AM

0

There was a Ruby Quiz on this a little while back, or at least, I saw
it a little while back. Might be in the Ruby Quiz book, I'm not sure.
Basically it was about creating serializable Procs, and most solutions
just wrapped Proc in classes which returned the Proc in most cases and
a string in the event of deserialization.

Long story short, if you want to do this, I believe you do have to
wrap Proc; there's nothing in Proc itself to store the code. Although
I don't know too much about what Facets or local_variables, so those
solutions could be better.

On 1/8/07, Marc Soda <marcantoniosr@gmail.com> wrote:
> Hey all,
>
> Is there a way to get the code (or, more importantly, the variables)
> out of a block associated with a Proc? For example:
>
> foo = lamdba { a = 'bar'; puts a }
> p foo.source
>
> produces "a = 'bar'; puts a"
>
> I thought about extending Proc and saving the block in initialize, but
> this seems like a lot of overhead if you have a lot of Procs.
>
> Any better ideas?
>
> Thanks,
> Marc
>
>


--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...

Eric Hodel

1/9/2007 4:46:00 PM

0

On Jan 8, 2007, at 19:35, Marc Soda wrote:
> Is there a way to get the code (or, more importantly, the variables)
> out of a block associated with a Proc? For example:
>
> foo = lamdba { a = 'bar'; puts a }
> p foo.source
>
> produces "a = 'bar'; puts a"
>
> I thought about extending Proc and saving the block in initialize, but
> this seems like a lot of overhead if you have a lot of Procs.
>
> Any better ideas?

Use the ruby2ruby gem.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Marc Soda

1/10/2007 2:52:00 AM

0

On 1/8/07, Trans <transfire@gmail.com> wrote:
>
> Marc Soda wrote:
> > Hey all,
> >
> > Is there a way to get the code (or, more importantly, the variables)
> > out of a block associated with a Proc? For example:
> >
> > foo = lamdba { a = 'bar'; puts a }
> > p foo.source
> >
> > produces "a = 'bar'; puts a"
> >
> > I thought about extending Proc and saving the block in initialize, but
> > this seems like a lot of overhead if you have a lot of Procs.
> >
> > Any better ideas?
>
> Not the source, but you can use local_varaibles against the binding of
> the proc. Check out Facets' binding methods too which can make it
> easier.
>
> http://facets.rub...
>
> T.
>
>
>
Since I'm really only interested in the variables this is perfect. Thanks!

Marc