# Proc code

**URL:** <https://rubytalk.org/t/proc-code/34311>\
**Category:** ruby-talk\
**Created:** [9 January 2007 03:35 UTC](https://rubytalk.org/t/proc-code/34311 "2007-01-09T03:35:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc3](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Marc3](https://rubytalk.org/u/Marc3)\
**Post date:** [9 January 2007 03:35 UTC](https://rubytalk.org/t/proc-code/34311/1 "2007-01-09T03:35:46Z")

</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:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [9 January 2007 04:36 UTC](https://rubytalk.org/t/proc-code/34311/2 "2007-01-09T04:36:15Z")

</div>

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.

&nbsp;&nbsp;[http://facets.rubyforge.org](http://facets.rubyforge.org)

T.

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [9 January 2007 08:36 UTC](https://rubytalk.org/t/proc-code/34311/3 "2007-01-09T08:36:23Z")

</div>

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.gilesgoatboy.org](http://www.gilesgoatboy.org)
> 
> > **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
> >
> > half-disavowed old blog. new blog at gilesbowkett.com
> 
> [http://gilesgoatboy.blogspot.com](http://gilesgoatboy.blogspot.com)

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [9 January 2007 16:45 UTC](https://rubytalk.org/t/proc-code/34311/4 "2007-01-09T16:45:50Z")

</div>

Use the ruby2ruby gem.

> **···**
>
> 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?
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)
> 
> I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![Marc3](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Marc3](https://rubytalk.org/u/Marc3)\
**Post date:** [10 January 2007 02:51 UTC](https://rubytalk.org/t/proc-code/34311/5 "2007-01-10T02:51:53Z")

</div>

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

Marc

> **···**
>
> 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.
> > 
> > &nbsp;&nbsp;[http://facets.rubyforge.org](http://facets.rubyforge.org)
> > 
> > T.
