# Ruby reflection - get method definition as a string

**URL:** <https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327>\
**Category:** Ruby\
**Created:** [February 17, 2011, 7:56pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327 "2011-02-17T19:56:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 17, 2011, 7:56pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/1 "2011-02-17T19:56:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [February 17, 2011, 8:41pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/2 "2011-02-17T20:41:04Z")

</div>

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

A very non-elegant, trivial way using Ruby’s DATA stream 😉 :

# test.rb

def foo  
puts “hi”  
end

flag = nil  
DATA.tap{|x| x.rewind}.readlines.each do |line|  
flag = true if line =~ /^def/  
puts line if flag  
flag = false if line =~ /^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.

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [February 17, 2011, 11:54pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/3 "2011-02-17T23:54:40Z")

</div>

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

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

In Ruby 1.9, you can retrieve the place where the method was defined  
using #source\_location:

def foo

end

method(:foo).source\_location #=\> [“file.rb”, 1]

The same goes for procs:

def foo(&block)  
puts block.source\_location.inspect  
end

foo do

end #=\> [“file.rb”, 5]

Regards,  
Florian

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [February 18, 2011, 1:04am UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/4 "2011-02-18T01:04:44Z")

</div>

On Thu, Feb 17, 2011 at 4:53 PM, Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > or .constants), but I’m not seeing how to do this.  
> > > flag = true if line =~ /^def/  
> > > puts line if flag  
> > > flag = false if line =~ /^end/  
> > > end  
> > > **END**

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

> In Ruby 1.9, you can retrieve the place where the method was defined using  
> #source\_location:  
> puts block.source\_location.inspect  
> end
> 
> foo do
> 
> end #=\> [“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?

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [February 18, 2011, 3:30pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/5 "2011-02-18T15:30:42Z")

</div>

Adam L. 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 ([GitHub - quix/live\_ast: Live abstract syntax trees of methods and procs. · GitHub](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.

---

<div class="post-metadata">

**Author:** ![b1gtuna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8dc957/32.png) [@b1gtuna](https://www.ruby-forum.com/u/b1gtuna)\
**Post date:** [February 18, 2011, 3:55pm UTC](https://www.ruby-forum.com/t/ruby-reflection-get-method-definition-as-a-string/203327/6 "2011-02-18T15:55:07Z")

</div>

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 F.  
Principal Consultant, BitsBuilder  
LI: [http://www.linkedin.com/in/johnxf](http://www.linkedin.com/in/johnxf)  
SO: [http://stackoverflow.com/users/75170/](http://stackoverflow.com/users/75170/)

On Fri, Feb 18, 2011 at 09:30, James M. Lawrence
