# Getting the names of classes defined in a ruby script

**URL:** <https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580>\
**Category:** Ruby\
**Created:** [November 8, 2007, 5:52am UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580 "2007-11-08T05:52:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 8, 2007, 5:52am UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580/1 "2007-11-08T05:52:35Z")

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

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 8, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580/2 "2007-11-08T17:22:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 8, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580/3 "2007-11-08T19:02:42Z")

</div>

On Nov 7, 2007, at 9:52 PM, Thilina B. 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/).

the basis is:

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

p Class.es

BEGIN {  
class Class  
ES = []  
def es() ES end  
def inherited(other) es \<\< other end  
end  
}

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

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

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 9, 2007, 4:23am UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580/4 "2007-11-09T04:23:23Z")

</div>

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

regs,  
buddhika

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 8, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/getting-the-names-of-classes-defined-in-a-ruby-script/119580/5 "2007-11-08T19:49:13Z")

</div>

Thilina B. 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  
Seen\_classes = []

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

def Seen\_classes  
Seen\_classes  
end  
end

class Dog  
end

class Monkey  
end

module Food  
end

PI = 3.14

def meth1  
end

p Class.Seen\_classes

–output:–  
[“Dog”, “Monkey”]
