Skip to content

Instantly share code, notes, and snippets.

@CootCraig
Created December 4, 2010 22:23
Show Gist options
  • Save CootCraig/728544 to your computer and use it in GitHub Desktop.
Save CootCraig/728544 to your computer and use it in GitHub Desktop.
Show the ancestor classes of an object
module TryItOut
class << self
def ancestors(o)
a = []
a << ((o.class == Class) ? o : o.class)
loop {
break if a[-1].nil?
a << a[-1].superclass
}
a
end
end
end
h = TryItOut.ancestors(File)
puts h.to_s
# TryItOut.ancestors(:a) #=> [Symbol, Object, BasicObject, nil]
# TryItOut.ancestors('') #=> [String, Object, BasicObject, nil]
# TryItOut.ancestors(String) #=> [String, Object, BasicObject, nil]
# TryItOut.ancestors(99) #=> [Fixnum, Integer, Numeric, Object, BasicObject, nil]
# TryItOut.ancestors(File) #=> [File, IO, Object, BasicObject, nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment