# Finding classes

**URL:** <https://www.ruby-forum.com/t/finding-classes/66952>\
**Category:** Ruby\
**Created:** [August 30, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/finding-classes/66952 "2006-08-30T16:23:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@pazu](https://www.ruby-forum.com/u/pazu)\
**Post date:** [August 30, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/finding-classes/66952/1 "2006-08-30T16:23:17Z")

</div>

I need to find classes that either:

1. Includes a specific module

or

1. Includes a specfic string in the class name

Either of the two is fine. This is in a Rails environment where I want  
to  
find all classes using one of the two rules above. The problem is that  
the  
classes I want to find probably aren’t “required” at the time of  
searching  
(ie ObjectSpace#each\_object doesn’t find them as far as I can tell and  
Module#included hasn’t been triggered).

Is it possible to do this?

Best regards,

Marcus

Tried this (doesn’t work):

```
class_names = Array.new
ObjectSpace.each_object { |obj| class_names << obj.name if 

```

obj.is\_a?(Class)  
&& obj.name.include?(“ChannelBehaviour”) }  
behaviours = classes.uniq.map { |name| Class.const\_get(name) }

---

<div class="post-metadata">

**Author:** ![pazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@pazu](https://www.ruby-forum.com/u/pazu)\
**Post date:** [August 30, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/finding-classes/66952/2 "2006-08-30T16:57:22Z")

</div>

Marcus B. wrote:

> I need to find classes that either:
> 
> 1. Includes a specific module
> 
> or
> 
> 1. Includes a specfic string in the class name

require ‘enumerator’

ObjectSpace.enum\_for(:each\_object, Class).select { |x|  
x.ancestors.include?(Module) }

ObjectSpace.enum\_for(:each\_object, Class).select { |x| x.name =~  
/String/ }

---

<div class="post-metadata">

**Author:** ![pazu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@pazu](https://www.ruby-forum.com/u/pazu)\
**Post date:** [August 30, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/finding-classes/66952/3 "2006-08-30T17:13:23Z")

</div>

On 8/30/06, Florian F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Marcus B. wrote:
> 
> require ‘enumerator’
> 
> ObjectSpace.enum\_for(:each\_object, Class).select { |x|  
> x.ancestors.include?(Module) }
> 
> ObjectSpace.enum\_for(:each\_object, Class).select { |x| x.name =~ /String/  
> }

Thanks for the answer. Unfortunately this won’t work as I explained in  
my  
previous mail. The classes aren’t required yet, only on the load path  
(and  
are auto-required by Rails when needed as I understand it), so I need to  
find them there somehow.

/Marcus
