Forum: Ruby which class defined

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daf54d45d90f3cfa77f8b2ba778af15a?d=identicon&s=25 raving_ruby_rider (Guest)
on 2006-02-15 14:55
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-02-15 16:39
(Received via mailing list)
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://codeforpeople.com/lib/ruby/dynaload/dynaloa...

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
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-02-15 16:42
(Received via mailing list)
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).
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-02-15 22:32
(Received via mailing list)
ara.t.howard@noaa.gov 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://codeforpeople.com/lib/ruby/dynaload/dynaloa...

There's also
http://raa.ruby-lang.org/project/script/
http://redshift.sourceforge.net/script/doc/index.html
D63c268960051bc17a310aa29fffd979?d=identicon&s=25 Dave Cantrell (Guest)
on 2006-02-16 03:53
(Received via mailing list)
Ross Bamford 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
Daf54d45d90f3cfa77f8b2ba778af15a?d=identicon&s=25 raving_ruby_rider (Guest)
on 2006-02-16 10:40
(Received via mailing list)
thx 2 all.

i like the Script encapsulation
851246810c70dbfcc1815c636b054562?d=identicon&s=25 George Ogata (Guest)
on 2006-02-16 14:09
(Received via mailing list)
Dave Cantrell <ruby-talk@deadcantrant.com> 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.
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-02-16 15:31
(Received via mailing list)
George Ogata 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.cafepress.com/mathomhouse.4631975
This topic is locked and can not be replied to.