# Looking at a Class's source

**URL:** <https://rubytalk.org/t/looking-at-a-classs-source/12639>\
**Category:** ruby-talk\
**Created:** [18 July 2004 17:24 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639 "2004-07-18T17:24:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [18 July 2004 17:24 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639/1 "2004-07-18T17:24:39Z")

</div>

Is there a way to look at the source of a class? E.g.

class Foo  
&nbsp;&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# something  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# something else  
&nbsp;&nbsp;&nbsp;end  
end

[0] Foo.source  
=\>class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# something  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# something else  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
[1]

Even being able to look at a method's source would be helpful, e.g.

[2] Foo.bar.source  
=\> def bar  
&nbsp;&nbsp;&nbsp;# something  
end  
[3]

Pretty printing is not necessary. I've looked at the built-in classes  
and modules and at tha standard library, and haven't been able to find  
anything related to this. If there is no way to do such thing then a  
simple "no" is sufficient, I'm just curious as to whether it is  
possible or not. Cheers!  
&nbsp;&nbsp;&nbsp;-CWS

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [18 July 2004 19:51 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639/2 "2004-07-18T19:51:46Z")

</div>

Not without going back to the actual source files. Ruby does not keep  
any form of the source itself around after parsing.

-austin

> **···**
>
> On Mon, 19 Jul 2004 02:24:39 +0900, Claus Spitzer \<docboobenstein@gmail.com\> wrote:
> 
> > Is there a way to look at the source of a class? E.g.

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [18 July 2004 20:24 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639/3 "2004-07-18T20:24:52Z")

</div>

Alright, understandable. Now the one question that remains (sorry for  
being nit-picky) is if it would be possible to desonstruct objects.  
After all, there has to be some place (possibly the metaclass) where  
it says that Foo.bar

class Foo  
def bar( in )  
&nbsp;&nbsp;&nbsp;in + 1  
end  
end

does in+1, right? Cheers  
&nbsp;&nbsp;&nbsp;-CWS

> **···**
>
> On Mon, 19 Jul 2004 04:51:46 +0900, Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > On Mon, 19 Jul 2004 02:24:39 +0900, Claus Spitzer \> \<docboobenstein@gmail.com\> wrote:  
> > \> Is there a way to look at the source of a class? E.g.
> > 
> > Not without going back to the actual source files. Ruby does not keep  
> > any form of the source itself around after parsing.
> > 
> > -austin

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [18 July 2004 20:54 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639/4 "2004-07-18T20:54:48Z")

</div>

I'm not understanding your question. Ruby interprets the code into an  
AST; if you know how to read the AST, then you may be able to figure  
that out. However, you're not going to be able to do that in Ruby  
"proper" (you'll need something like evil.rb which uses Ruby/DL, or  
some of the other tools out there).

-austin

> **···**
>
> On Mon, 19 Jul 2004 05:24:52 +0900, Claus Spitzer \<docboobenstein@gmail.com\> wrote:
> 
> > Alright, understandable. Now the one question that remains (sorry for  
> > being nit-picky) is if it would be possible to desonstruct objects.  
> > After all, there has to be some place (possibly the metaclass) where  
> > it says that Foo.bar
> > 
> > class Foo  
> > def bar( in )  
> > &nbsp;&nbsp;&nbsp;in + 1  
> > end  
> > end
> > 
> > does in+1, right? Cheers
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [18 July 2004 21:47 UTC](https://rubytalk.org/t/looking-at-a-classs-source/12639/5 "2004-07-18T21:47:38Z")

</div>

Aha. I'll look into it, thanks.

> **···**
>
> On Mon, 19 Jul 2004 05:54:48 +0900, Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > On Mon, 19 Jul 2004 05:24:52 +0900, Claus Spitzer \> \<docboobenstein@gmail.com\> wrote:  
> > \> Alright, understandable. Now the one question that remains (sorry for  
> > \> being nit-picky) is if it would be possible to desonstruct objects.  
> > \> After all, there has to be some place (possibly the metaclass) where  
> > \> it says that Foo.bar  
> > \>  
> > \> class Foo  
> > \> def bar( in )  
> > \> in + 1  
> > \> end  
> > \> end  
> > \>  
> > \> does in+1, right? Cheers
> > 
> > I'm not understanding your question. Ruby interprets the code into an  
> > AST; if you know how to read the AST, then you may be able to figure  
> > that out. However, you're not going to be able to do that in Ruby  
> > "proper" (you'll need something like evil.rb which uses Ruby/DL, or  
> > some of the other tools out there).
> > 
> > -austin  
> > --  
> > Austin Ziegler \* halostatue@gmail.com  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca
