# Getting the names of classes defined in a ruby script

**URL:** <https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004>\
**Category:** ruby-talk\
**Created:** [8 November 2007 04:52 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004 "2007-11-08T04:52:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thilina\_Buddhika](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@Thilina\_Buddhika](https://rubytalk.org/u/Thilina_Buddhika)\
**Post date:** [8 November 2007 04:52 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004/1 "2007-11-08T04:52:41Z")

</div>

I want to get the names of the classes defined in a Ruby script. Is  
there any possible way of doing this ?

thanks!

regs,  
buddhika

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [8 November 2007 16:22 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004/2 "2007-11-08T16:22:08Z")

</div>

Thilina Buddhika wrote:

> I want to get the names of the classes defined in a Ruby script. Is  
> there any possible way of doing this ?
> 
> thanks!
> 
> regs,  
> buddhika

Probably not the best way of doing it but...

file = File.readlines("rubyscript.rb")  
res =   
file.each do |x|  
res \<\< x  
end

# then using regular expressions you could filter out the upper case  
words  
# to be more accurate you could develop a gsub or iteration that  
includes the  
# word 'class'

i forget the reg. exp. for it but i believe its along the lines of  
/[A-Z]/  
or close to that

- Mac

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [8 November 2007 18:00 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004/3 "2007-11-08T18:00:18Z")

</div>

the basis is:

cfp:~ \> cat a.rb  
class A;end  
class B;end  
class C \< B; end

p Class.es

BEGIN {  
&nbsp;&nbsp;&nbsp;class Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ES =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def es() ES end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def inherited(other) es \<\< other end  
&nbsp;&nbsp;&nbsp;end  
}

cfp:~ \> ruby a.rb  
[A, B, C]

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Nov 7, 2007, at 9:52 PM, Thilina Buddhika wrote:
> 
> > I want to get the names of the classes defined in a Ruby script. Is  
> > there any possible way of doing this ?
> > 
> > thanks!
> > 
> > regs,  
> > buddhika  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [8 November 2007 18:49 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004/4 "2007-11-08T18:49:12Z")

</div>

Thilina Buddhika wrote:

> I want to get the names of the classes defined in a Ruby script. Is  
> there any possible way of doing this ?

Try this:

class Class  
&nbsp;&nbsp;Seen\_classes =

&nbsp;&nbsp;def inherited(class\_obj)  
&nbsp;&nbsp;&nbsp;&nbsp;Seen\_classes \<\< class\_obj.name  
&nbsp;&nbsp;end

&nbsp;&nbsp;def Seen\_classes  
&nbsp;&nbsp;&nbsp;&nbsp;Seen\_classes  
&nbsp;&nbsp;end  
end

class Dog  
end

class Monkey  
end

module Food  
end

PI = 3.14

def meth1  
end

p Class.Seen\_classes

--output:--  
["Dog", "Monkey"]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Thilina\_Buddhika](https://avatars.discourse-cdn.com/v4/letter/t/e495f1/32.png) [@Thilina\_Buddhika](https://rubytalk.org/u/Thilina_Buddhika)\
**Post date:** [9 November 2007 03:23 UTC](https://rubytalk.org/t/getting-the-names-of-classes-defined-in-a-ruby-script/42004/5 "2007-11-09T03:23:31Z")

</div>

Hi friends,  
It works fine,...thanks a lot !!!!!

regs,  
buddhika

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
