# Print a method definition from irb?

**URL:** <https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488>\
**Category:** Ruby\
**Created:** [December 8, 2007, 3:02am UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488 "2007-12-08T03:02:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Seth\_C](https://www.ruby-forum.com/u/Seth_C)\
**Post date:** [December 8, 2007, 3:02am UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488/1 "2007-12-08T03:02:40Z")

</div>

Is it possible to reflect the text of a dynamically generated method  
definition?

I’m debugging some ActiveRecord code and it would be very helpful to  
dump the text of some of the generated methods. For example, say I  
have this:

class Foo \< ActiveRecord::Base  
has\_one :bar  
end

AR associations generates a whole host of methods, Foo#bar,  
Foo#create\_bar, etc., and the exact definitions depend on the options  
that I passed to has\_one. I can walk through associations.rb and  
friends to reconstruct what I think the methods look like but that is  
slow and error prone.

There must be an easier way, this is Ruby after all!

Thanks,  
.seth

---

<div class="post-metadata">

**Author:** ![Seth\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Seth\_C](https://www.ruby-forum.com/u/Seth_C)\
**Post date:** [December 8, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488/2 "2007-12-08T17:54:36Z")

</div>

On 12/7/07, Seth C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to reflect the text of a dynamically generated method  
> definition?

Not without changing the code which generates the method itself.

That would make a kickass plugin, though.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![Seth\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Seth\_C](https://www.ruby-forum.com/u/Seth_C)\
**Post date:** [December 9, 2007, 1:24am UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488/3 "2007-12-09T01:24:41Z")

</div>

Have you tried Ruby2Ruby? It uses ParseTree to translate ruby code in  
memory into ruby code in strings.

-Bryan

---

<div class="post-metadata">

**Author:** ![Seth\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Seth\_C](https://www.ruby-forum.com/u/Seth_C)\
**Post date:** [December 9, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488/4 "2007-12-09T20:21:39Z")

</div>

Brilliant, exactly what I was looking for! Thanks, Bryan.

.seth

---

<div class="post-metadata">

**Author:** ![Seth\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Seth\_C](https://www.ruby-forum.com/u/Seth_C)\
**Post date:** [December 9, 2007, 7:59am UTC](https://www.ruby-forum.com/t/print-a-method-definition-from-irb/122488/5 "2007-12-09T07:59:08Z")

</div>

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

> Have you tried Ruby2Ruby? It uses ParseTree to translate ruby code in  
> memory into ruby code in strings.

Woah.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)
