Created
          December 4, 2010 22:23 
        
      - 
      
- 
        Save CootCraig/728544 to your computer and use it in GitHub Desktop. 
    Show the ancestor classes of an object
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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