# Proc code

**URL:** <https://www.ruby-forum.com/t/proc-code/81468>\
**Category:** Ruby\
**Created:** [January 9, 2007, 4:36am UTC](https://www.ruby-forum.com/t/proc-code/81468 "2007-01-09T04:36:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [January 9, 2007, 4:36am UTC](https://www.ruby-forum.com/t/proc-code/81468/1 "2007-01-09T04:36:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [January 9, 2007, 5:37am UTC](https://www.ruby-forum.com/t/proc-code/81468/2 "2007-01-09T05:37:26Z")

</div>

Marc S. 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](http://facets.rubyforge.org)

T.

---

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [January 9, 2007, 9:36am UTC](https://www.ruby-forum.com/t/proc-code/81468/3 "2007-01-09T09:36:45Z")

</div>

There was a Ruby Q. on this a little while back, or at least, I saw  
it a little while back. Might be in the Ruby Q. 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.

---

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [January 10, 2007, 3:52am UTC](https://www.ruby-forum.com/t/proc-code/81468/4 "2007-01-10T03:52:20Z")

</div>

On 1/8/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > produces “a = ‘bar’; puts a”  
> > [http://facets.rubyforge.org](http://facets.rubyforge.org)
> 
> T.

Since I’m really only interested in the variables this is perfect.  
Thanks!

Marc

---

<div class="post-metadata">

**Author:** ![mschipperheyn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mschipperheyn](https://www.ruby-forum.com/u/mschipperheyn)\
**Post date:** [January 9, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/proc-code/81468/5 "2007-01-09T17:46:11Z")

</div>

On Jan 8, 2007, at 19:35, Marc S. wrote:

> Any better ideas?

Use the ruby2ruby gem.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!
