# How can I dynamically get the source of an object's method?

**URL:** <https://www.ruby-forum.com/t/how-can-i-dynamically-get-the-source-of-an-objects-method/76017>\
**Category:** Ruby\
**Created:** [November 15, 2006, 9:37am UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-get-the-source-of-an-objects-method/76017 "2006-11-15T09:37:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 15, 2006, 9:37am UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-get-the-source-of-an-objects-method/76017/1 "2006-11-15T09:37:23Z")

</div>

Hello:

I’ve been wondering if it’s possible to get an object to print out the  
source code of a method into a string.

It seems like it _should_ be possible, but it’s kind of a wacky idea. I  
haven’t seen it mentioned anywhere.

Thanks in advance,  
David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 15, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/how-can-i-dynamically-get-the-source-of-an-objects-method/76017/2 "2006-11-15T15:34:15Z")

</div>

On Nov 15, 2006, at 2:37 AM, David L. wrote:

> I’ve been wondering if it’s possible to get an object to print out the  
> source code of a method into a string.

This isn’t easy in the general sense with Ruby, because you can use  
code to write, or even change, code.

However, some of the tricks for the SerializableProc Ruby Q. might  
give you ideas:

[http://www.rubyquiz.com/quiz38.html](http://www.rubyquiz.com/quiz38.html)

See Florian G.'s solution, for example.

James Edward G. II
