Module and class hierarchy : inheritance « Class « Ruby






Module and class hierarchy


module A
end                 # Empty module
module B 
   include A 
end                 # Module B includes A

class C 
   include B 
end                 # Class C includes module B

puts C < B               # => true: C includes B
puts B < A               # => true: B includes A
puts C < A               # => true
puts Fixnum < Integer    # => true: all fixnums are integers
puts Integer <Comparable # => true: integers are comparable
puts Integer < Fixnum    # => false: not all integers are fixnums
puts String < Numeric    # => nil: strings are not numbers

puts A.ancestors         # => [A]
puts B.ancestors         # => [B, A]
puts C.ancestors         # => [C, B, A, Object, Kernel]
puts String.ancestors    # => [String, Enumerable, Comparable, Object, Kernel]
                    # Note: in Ruby 1.9 String is no longer Enumerable

puts C.include?(B)       # => true
puts C.include?(A)       # => true
puts B.include?(A)       # => true
puts A.include?(A)       # => false
puts A.include?(B)       # => false

puts A.included_modules  # => []
puts B.included_modules  # => [A]
puts C.included_modules  # => [B, A, Kernel]

 








Related examples in the same category

1.Basing one class on another is called inheritance.
2.Extends class
3.how inheritance works in code form
4.Basing One Class on Another: a Demo
5.different types of people
6.Structuring Your Pets Logically
7.If the class Name were in a different file, you would just require that file first
8.Add new constructor
9.Subclass Array class