# Which class defined

**URL:** <https://www.ruby-forum.com/t/which-class-defined/53859>\
**Category:** Ruby\
**Created:** [February 15, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859 "2006-02-15T14:55:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 15, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/1 "2006-02-15T14:55:08Z")

</div>

Hi,

I need to load scripts dynamically.

Is there a way to know about what  
classes were defined by loading a script?

If not, can the Ruby system be asked for a collection  
of subclasses for a particular Class?

thx

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 15, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/2 "2006-02-15T16:39:16Z")

</div>

On Wed, 15 Feb 2006, raving\_ruby\_rider wrote:

> Is there a way to know about what  
> classes were defined by loading a script?

see dynaload

[http://rubyforge.org/frs/?group\_id=1024&release\_id=3786](http://rubyforge.org/frs/?group_id=1024&release_id=3786)  
[http://codeforpeople.com/lib/ruby/dynaload/dynaload-0.0.0/README](http://codeforpeople.com/lib/ruby/dynaload/dynaload-0.0.0/README)

is use it for the very same.

> If not, can the Ruby system be asked for a collection of subclasses for a  
> particular Class?

```
 harp:~ > cat a.rb
 class C
   class << self
     def children() @children ||= [] end
     def inherited(other) children << other and super end
     def descendants() children.inject([]){|d,c| d.push(c, 

```

\*c.descendants)} end  
end  
end

```
 load "b.rb"

 p C::children
 p C::descendants
 p B::children
 p B::descendants

 harp:~ > cat b.rb
 class B < C; end
 class D < B; end

 harp:~ > ruby a.rb
 [B]
 [B, D]
 [D]
 [D]

```

regards.

-a

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 15, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/3 "2006-02-15T16:42:21Z")

</div>

On Wed, 2006-02-15 at 22:53 +0900, raving\_ruby\_rider wrote:

> Is there a way to know about what  
> classes were defined by loading a script?

I’ve done something like this before:

```
    sc, ec = [], []
    # => [[], []]

    ObjectSpace.each_object(Class) { |c| sc << c }
    # => 471
    require 'generator'
    # => true
    ObjectSpace.each_object(Class) { |c| ec << c }
    # => 473

    new_classes = ec - sc
    # => [SyncEnumerator, Generator]

```

> If not, can the Ruby system be asked for a collection  
> of subclasses for a particular Class?

See above (the argument to each\_object).

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 15, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/4 "2006-02-15T22:32:00Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On Wed, 15 Feb 2006, raving\_ruby\_rider wrote:
> 
> > Is there a way to know about what  
> > classes were defined by loading a script?
> 
> see dynaload
> 
> [http://rubyforge.org/frs/?group\_id=1024&release\_id=3786](http://rubyforge.org/frs/?group_id=1024&release_id=3786)  
> [http://codeforpeople.com/lib/ruby/dynaload/dynaload-0.0.0/README](http://codeforpeople.com/lib/ruby/dynaload/dynaload-0.0.0/README)

There’s also  
[http://raa.ruby-lang.org/project/script/](http://raa.ruby-lang.org/project/script/)  
[http://redshift.sourceforge.net/script/doc/index.html](http://redshift.sourceforge.net/script/doc/index.html)

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 16, 2006, 10:40am UTC](https://www.ruby-forum.com/t/which-class-defined/53859/5 "2006-02-16T10:40:44Z")

</div>

thx 2 all.

i like the Script encapsulation

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 16, 2006, 3:53am UTC](https://www.ruby-forum.com/t/which-class-defined/53859/6 "2006-02-16T03:53:11Z")

</div>

Ross B. wrote:

> > If not, can the Ruby system be asked for a collection  
> > of subclasses for a particular Class?
> 
> See above (the argument to each\_object).

Heh. If you make your terminal black and green and run this:

ObjectSpace.each\_object(Object) { |o| puts o }

… you will see the truth of the Matrix. 🙂

-dave

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 16, 2006, 2:09pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/7 "2006-02-16T14:09:10Z")

</div>

Dave C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Heh. If you make your terminal black and green and run this:
> 
> ObjectSpace.each\_object(Object) { |o| puts o }
> 
> … you will see the truth of the Matrix. 🙂
> 
> -dave

You get used to it. I don’t even see the code. All I see is Symbol,  
String, Array, …

Cypher.

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 16, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/which-class-defined/53859/8 "2006-02-16T15:31:01Z")

</div>

George O. wrote:

> You get used to it. I don’t even see the code. All I see is Symbol,  
> String, Array, …

These show what gets mixed in from where:  
[http://www.insula.cz/dali/material/rubycl/](http://www.insula.cz/dali/material/rubycl/)

> **[The Ruby Core (tentative image) | CafePress](https://www.cafepress.com/mathomhouse.4631975)**
>
> Shop The Ruby Core (tentative image) designed by The Mathom-House. Lots of different size and color combinations to choose from. ✓Free Returns ✓High Quality Printing ✓Fast Shipping
