Skip to content

Instantly share code, notes, and snippets.

@jarmo
Created Aug 1, 2010
Embed
What would you like to do?
class Object
def descendant?(parent)
parent = parent.class unless parent.is_a?(Class) || parent.is_a?(Module)
self.class.to_s.index("#{parent}::") == 0
end
end
module MyModule
module AnotherModule
class MyClass
end
class AnotherClass
end
end
end
module MyAnotherModule
end
m = MyModule::AnotherModule::MyClass.new
p m.descendant?(MyModule)
p m.descendant?(MyModule::AnotherModule)
p m.descendant?(MyModule::AnotherModule::MyClass)
p m.descendant?(m)
a = MyModule::AnotherModule::AnotherClass.new
p m.descendant?(a)
p m.descendant?(MyAnotherModule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment