which class defined

R

raving_ruby_rider

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
 
A

ara.t.howard

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/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, D]
[D]
[D]


regards.

-a
 
R

Ross Bamford

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).
 
D

Dave Cantrell

Ross said:
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).

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
 
G

George Ogata

Dave Cantrell said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,160
Latest member
KieranKisc

Latest Threads

Top