Forum: Ruby Proc code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5892288702862ce1bf3fe2ae54745e72?d=identicon&s=25 Marc Soda (Guest)
on 2007-01-09 04:36
(Received via mailing list)
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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-01-09 05:37
(Received via mailing list)
Marc Soda wrote:
> 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.
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-01-09 09:36
(Received via mailing list)
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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-09 17:46
(Received via mailing list)
On Jan 8, 2007, at 19:35, Marc Soda wrote:
>
> Any better ideas?

Use the ruby2ruby gem.

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

I LIT YOUR GEM ON FIRE!
5892288702862ce1bf3fe2ae54745e72?d=identicon&s=25 Marc Soda (Guest)
on 2007-01-10 03:52
(Received via mailing list)
On 1/8/07, Trans <transfire@gmail.com> wrote:
> > produces "a = 'bar'; puts a"
>   http://facets.rubyforge.org
>
> T.
>
>
>
Since I'm really only interested in the variables this is perfect.
Thanks!

Marc
This topic is locked and can not be replied to.