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

On 12/7/07, Seth C. [email protected] 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
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

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

.seth

On 12/8/07, Bryan D. [email protected] 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
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs