# Determining the Source File of a Class

**URL:** <https://www.ruby-forum.com/t/determining-the-source-file-of-a-class/192108>\
**Category:** Ruby\
**Created:** [July 23, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/determining-the-source-file-of-a-class/192108 "2010-07-23T22:44:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alangano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/94ad74/32.png) [@alangano](https://www.ruby-forum.com/u/alangano)\
**Post date:** [July 23, 2010, 10:44pm UTC](https://www.ruby-forum.com/t/determining-the-source-file-of-a-class/192108/1 "2010-07-23T22:44:48Z")

</div>

I’m trying to debug a bug where I seem to have a class that has two  
defintions, the one I know of, and another one, somewhere, the one I’m  
getting when I attempt to use the class.

Given a class, is there a way to get name of the file in which it was  
defined?

MyClass.defintion\_file

Something like that?

---

<div class="post-metadata">

**Author:** ![alangano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/94ad74/32.png) [@alangano](https://www.ruby-forum.com/u/alangano)\
**Post date:** [July 23, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/determining-the-source-file-of-a-class/192108/2 "2010-07-23T23:07:03Z")

</div>

On Jul 23, 2010, at 13:40 , Alan G. wrote:

> I’m trying to debug a bug where I seem to have a class that has two defintions, the one I know of, and another one, somewhere, the one I’m getting when I attempt to use the class.
> 
> Given a class, is there a way to get name of the file in which it was defined?
> 
> MyClass.defintion\_file
> 
> Something like that?

Classes in ruby are “open”, which means that they can be extended at any  
time. So there isn’t really a “definition” (at least, for a single  
location) of a given class.

You also have the case where something isn’t opened to have stuff added  
to it, but instead it can be extended by modules instead.

If you want, you can play with the reflective hooks in Module:  
method\_added, included, extended, etc.

---

<div class="post-metadata">

**Author:** ![alangano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/94ad74/32.png) [@alangano](https://www.ruby-forum.com/u/alangano)\
**Post date:** [July 23, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/determining-the-source-file-of-a-class/192108/3 "2010-07-23T23:31:04Z")

</div>

Ryan D. wrote:

> Classes in ruby are “open”, which means that they can be extended at any time. So there isn’t really a “definition” (at least, for a single location) of a given class.  
> Thank you. That answers my question.

> You also have the case where something isn’t opened to have stuff added to it, but instead it can be extended by modules instead.
> 
> If you want, you can play with the reflective hooks in Module: method\_added, included, extended, etc.

Sounds like fire hose, but I’m sure there’s a clever Ruby way to control  
the torrent.

Fortunately, I seem to have found the other definition that is being  
invoked using `find` and `grep`.
