print a method definition from irb?

Discussion in 'Ruby' started by Seth Cousins, Dec 8, 2007.

  1. Seth Cousins

    Seth Cousins Guest

    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
     
    Seth Cousins, Dec 8, 2007
    #1
    1. Advertising

  2. Giles Bowkett, Dec 8, 2007
    #2
    1. Advertising

  3. 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 <> 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
    >
     
    Bryan Duxbury, Dec 9, 2007
    #3
  4. Giles Bowkett, Dec 9, 2007
    #4
  5. Seth Cousins

    Seth Cousins Guest

    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 <> 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
    >>

    >
    >
     
    Seth Cousins, Dec 9, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keto
    Replies:
    0
    Views:
    964
  2. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    363
    David Cournapeau
    Dec 30, 2008
  3. Sam Stephenson
    Replies:
    1
    Views:
    229
    Andrew Walrond
    Jun 18, 2005
  4. Replies:
    1
    Views:
    162
    Florian GroƟ
    Oct 26, 2005
  5. anne001
    Replies:
    1
    Views:
    277
    anne001
    Jun 27, 2006
Loading...

Share This Page