Skip to content

Instantly share code, notes, and snippets.

@jarmo
Created August 1, 2010 19:03
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 jarmo/503646 to your computer and use it in GitHub Desktop.
Save jarmo/503646 to your computer and use it in GitHub Desktop.
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