Print a method definition from irb?

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

Not without changing the code which generates the method itself.

That would make a kickass plugin, though.

···

On 12/7/07, Seth Cousins <seth.cousins@gmail.com> wrote:

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

--
Giles Bowkett

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

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

-Bryan

···

On Dec 8, 2007, at 8:53 AM, Giles Bowkett wrote:

On 12/7/07, Seth Cousins <seth.cousins@gmail.com> 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 Bowkett

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

Woah.

···

On 12/8/07, Bryan Duxbury <bryan@rapleaf.com> wrote:

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

--
Giles Bowkett

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

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

.seth

···

On Dec 8, 2007, at 6:23 PM, Bryan Duxbury wrote:

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

-Bryan

On Dec 8, 2007, at 8:53 AM, Giles Bowkett wrote:

On 12/7/07, Seth Cousins <seth.cousins@gmail.com> 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 Bowkett

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