Ruby reflection - get method definition as a string

Discussion in 'Ruby' started by Laup-Dawg, Feb 17, 2011.

  1. Laup-Dawg

    Laup-Dawg Guest

    Hi Folks - I am running some ruby tests and would like to print out
    the actual test to the console or store it to a file. The tests are
    written as classes with a setup, run, and teardown method.
    So here's my question: is there a way to get the class definition or
    method definitions as a "string", so that I can print it out (to a
    file or console)?
    I know ruby supports a lot of reflection (like .methods
    or .constants), but I'm not seeing how to do this.
    Thanks!
    Laup-Dawg, Feb 17, 2011
    #1
    1. Advertising

  2. > Hi Folks - I am running some ruby tests and would like to print out
    > the actual test to the console or store it to a file. =A0The tests are
    > written as classes with a setup, run, and teardown method.
    > So here's my question: is there a way to get the class definition or
    > method definitions as a "string", so that I can print it out (to a
    > file or console)?
    > I know ruby supports a lot of reflection (like .methods
    > or .constants), but I'm not seeing how to do this.


    A very non-elegant, trivial way using Ruby's DATA stream ;) :

    # test.rb
    def foo
    puts "hi"
    end

    flag =3D nil
    DATA.tap{|x| x.rewind}.readlines.each do |line|
    flag =3D true if line =3D~ /^def/
    puts line if flag
    flag =3D false if line =3D~ /^end/
    end
    __END__

    $ ruby test.rb
    def foo
    puts "hi"
    end

    Obviously you will have to come up with better heuristics to determine
    the end of a method.

    --=20
    Anurag Priyam
    http://about.me/yeban/
    Anurag Priyam, Feb 17, 2011
    #2
    1. Advertising

  3. On Feb 17, 2011, at 8:40 PM, Anurag Priyam wrote:

    >> Hi Folks - I am running some ruby tests and would like to print out
    >> the actual test to the console or store it to a file. The tests are
    >> written as classes with a setup, run, and teardown method.
    >> So here's my question: is there a way to get the class definition or
    >> method definitions as a "string", so that I can print it out (to a
    >> file or console)?
    >> I know ruby supports a lot of reflection (like .methods
    >> or .constants), but I'm not seeing how to do this.

    >=20
    > A very non-elegant, trivial way using Ruby's DATA stream ;) :
    >=20
    > # test.rb
    > def foo
    > puts "hi"
    > end
    >=20
    > flag =3D nil
    > DATA.tap{|x| x.rewind}.readlines.each do |line|
    > flag =3D true if line =3D~ /^def/
    > puts line if flag
    > flag =3D false if line =3D~ /^end/
    > end
    > __END__
    >=20
    > $ ruby test.rb
    > def foo
    > puts "hi"
    > end
    >=20
    > Obviously you will have to come up with better heuristics to determine
    > the end of a method.
    >=20


    In Ruby 1.9, you can retrieve the place where the method was defined =
    using #source_location:

    def foo

    end
    =20
    method:)foo).source_location #=3D> ["file.rb", 1]

    The same goes for procs:

    def foo(&block)
    puts block.source_location.inspect
    end

    foo do

    end #=3D> ["file.rb", 5]

    Regards,
    Florian
    Florian Gilcher, Feb 17, 2011
    #3
  4. On Thu, Feb 17, 2011 at 4:53 PM, Florian Gilcher <> wro=
    te:
    >
    > On Feb 17, 2011, at 8:40 PM, Anurag Priyam wrote:
    >
    >>> Hi Folks - I am running some ruby tests and would like to print out
    >>> the actual test to the console or store it to a file. =A0The tests are
    >>> written as classes with a setup, run, and teardown method.
    >>> So here's my question: is there a way to get the class definition or
    >>> method definitions as a "string", so that I can print it out (to a
    >>> file or console)?
    >>> I know ruby supports a lot of reflection (like .methods
    >>> or .constants), but I'm not seeing how to do this.

    >>
    >> A very non-elegant, trivial way using Ruby's DATA stream ;) :
    >>
    >> # test.rb
    >> def foo
    >> =A0puts "hi"
    >> end
    >>
    >> flag =3D nil
    >> DATA.tap{|x| x.rewind}.readlines.each do |line|
    >> =A0flag =3D true if line =3D~ /^def/
    >> =A0puts line if flag
    >> =A0flag =3D false if line =3D~ /^end/
    >> end
    >> __END__


    Cool, I didn't realize you access the source file using tap and rewind.

    >>
    >> $ ruby test.rb
    >> def foo
    >> =A0puts "hi"
    >> end
    >>
    >> Obviously you will have to come up with better heuristics to determine
    >> the end of a method.
    >>

    >
    > In Ruby 1.9, you can retrieve the place where the method was defined usin=

    g #source_location:
    >
    > =A0def foo
    >
    > =A0end
    >
    > =A0method:)foo).source_location #=3D> ["file.rb", 1]
    >
    > The same goes for procs:
    >
    > =A0def foo(&block)
    > =A0 =A0puts block.source_location.inspect
    > =A0end
    >
    > =A0foo do
    >
    > =A0end #=3D> ["file.rb", 5]


    I just discovered #source_location a week or so ago, but I immediately
    wondered if there was a way to discern the end of a block or method. I
    never found one. Might one be added in a future Ruby?
    Eric Christopherson, Feb 18, 2011
    #4
  5. Adam Lauper wrote in post #982302:
    > Hi Folks - I am running some ruby tests and would like to print out
    > the actual test to the console or store it to a file. The tests are
    > written as classes with a setup, run, and teardown method.
    > So here's my question: is there a way to get the class definition or
    > method definitions as a "string", so that I can print it out (to a
    > file or console)?
    > I know ruby supports a lot of reflection (like .methods
    > or .constants), but I'm not seeing how to do this.
    > Thanks!


    I just released a gem which gives access to the source code of methods
    and procs (http://github.com/quix/live_ast). See the "to_ruby" section
    of the readme. Since the string returned by to_ruby is a parsed/unparsed
    version of your code, it probably won't match the original source (and
    comments will be gone), but it may suffice for your needs.

    --
    Posted via http://www.ruby-forum.com/.
    James M. Lawrence, Feb 18, 2011
    #5
  6. I haven't looked at James's gem, but if live_ast can map AST
    structures to lines in the source, then it'd be easy to determine
    which range of lines in a file is the interesting part. Then you'd
    know precisely what section to grab.

    ~ jf
    --
    John Feminella
    Principal Consultant, BitsBuilder
    LI: http://www.linkedin.com/in/johnxf
    SO: http://stackoverflow.com/users/75170/



    On Fri, Feb 18, 2011 at 09:30, James M. Lawrence
    <> wrote:
    > Adam Lauper wrote in post #982302:
    >> Hi Folks - I am running some ruby tests and would like to print out
    >> the actual test to the console or store it to a file. =C2=A0The tests ar=

    e
    >> written as classes with a setup, run, and teardown method.
    >> So here's my question: is there a way to get the class definition or
    >> method definitions as a "string", so that I can print it out (to a
    >> file or console)?
    >> I know ruby supports a lot of reflection (like .methods
    >> or .constants), but I'm not seeing how to do this.
    >> Thanks!

    >
    > I just released a gem which gives access to the source code of methods
    > and procs (http://github.com/quix/live_ast). See the "to_ruby" section
    > of the readme. Since the string returned by to_ruby is a parsed/unparsed
    > version of your code, it probably won't match the original source (and
    > comments will be gone), but it may suffice for your needs.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    John Feminella, Feb 18, 2011
    #6
    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. jerry051
    Replies:
    2
    Views:
    739
    Oliver Sturm
    Aug 2, 2005
  2. Jianli Shen
    Replies:
    1
    Views:
    582
    Victor Bazarov
    Mar 13, 2005
  3. Francis Hwang
    Replies:
    4
    Views:
    120
    Pit Capitain
    Mar 8, 2005
  4. Ezra Zygmuntowicz
    Replies:
    2
    Views:
    108
    Ezra Zygmuntowicz
    Jul 10, 2006
  5. Sirisha Pusapati

    Ruby Reflection Method Parameters

    Sirisha Pusapati, Jun 17, 2009, in forum: Ruby
    Replies:
    2
    Views:
    133
    Jörg W Mittag
    Jun 17, 2009
Loading...

Share This Page