Ancestor List in Ruby

April 4, 2008 at 4:45 am (ruby) (, )

wandering, i wanted to list ancestor list of a class, and also an object. like 1.superclasses
here’s how i do it

class Object
def self.superclasses
  a = [self]
  until a.last.nil?
    a << a.last.superclass
  end
  a.pop # to remove nil at the end of the list
  a
end
def superclasses
  self.class.superclasses
end
end


so

Fixnum.superclasses #=> [Fixnum Integer, Numeric, Object]
1.superclasses #=> [Fixnum Integer, Numeric, Object]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: