[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to get inherited class names ?

Pokkai Dokkai

3/20/2008 12:39:00 PM

how to get all inherited class names ?
--
Posted via http://www.ruby-....

3 Answers

WujcioL

3/20/2008 12:52:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

It's no that what you want but maybe it will help you:

a.class #=> return class
a.class.superclass #=>return superclass of class. Example:

class A
def a
"a"
end
end

class B < A
def b
"b"
end
end

beta = B.new
b.class.superclass #=> A

2008/3/20, Pokkai Dokkai <bad_good_lion@yahoo.com>:
>
> how to get all inherited class names ?
>
> --
> Posted via http://www.ruby-....
>
>

Jano Svitok

3/20/2008 1:31:00 PM

0

On Thu, Mar 20, 2008 at 1:39 PM, Pokkai Dokkai <bad_good_lion@yahoo.com> wrote:
> how to get all inherited class names ?

# Class helper functions
class Class

# Iterates over all subclasses (direct and indirect)
def each_subclass
ObjectSpace.each_object(Class) { | candidate |
yield candidate if candidate < self
}
end

# Returns an Array of subclasses (direct and indirect)
def subclasses
ret = []
each_subclass {|c| ret << c}
ret
end

# Returns an Array of direct subclasses
def direct_subclasses
ret = []
each_subclass {|c| ret << c if c.superclass == self }
ret
end
end

Robert Klemme

3/20/2008 5:32:00 PM

0

On 20.03.2008 13:39, Pokkai Dokkai wrote:
> how to get all inherited class names ?

irb(main):001:0> Fixnum.superclass
=> Integer
irb(main):002:0> Fixnum.ancestors
=> [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]
irb(main):003:0>

robert