Proc code

M

Marc Soda

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
 
T

Trans

Marc said:
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.rubyforge.org

T.
 
G

Giles Bowkett

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

Eric Hodel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top