how to get inherited class names ?

M

Mateusz Tybura

[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
 
J

Jano Svitok

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
 
R

Robert Klemme

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
 

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

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
Virus9283

Latest Threads

Top