Skip to content

Instantly share code, notes, and snippets.

@ged
Created April 14, 2011 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ged/919983 to your computer and use it in GitHub Desktop.
Save ged/919983 to your computer and use it in GitHub Desktop.
Module is_a?
#!/usr/bin/env ruby
class A
def initialize
puts "Initializing an A"
super
end
end
module B
def initialize
puts "Initializing a B"
super
end
end
module B2
include B
def initialize
puts "Initializing a B2"
super
end
end
class C < A
include B2
def initialize
puts "Initializing a C"
super
end
end
obj = C.new
[ C, B, B2, A ].each do |mod|
puts "%p is_a %p? %p" % [ obj, mod, obj.is_a?(mod) ]
end
# $ ruby ~/source/ruby/misc/modinheritance.rb
# Initializing a C
# Initializing a B2
# Initializing a B
# Initializing an A
# #<C:0x0000010086a690> is_a C? true
# #<C:0x0000010086a690> is_a B? true
# #<C:0x0000010086a690> is_a B2? true
# #<C:0x0000010086a690> is_a A? true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment