From: "David A. Black" Date: 2010-08-02T05:08:30+09:00 Subject: Re: how to check if some object is included in some module? Hi -- On Mon, 2 Aug 2010, Jarmo Pertman wrote: > I know that there are methods #kind_of?/#is_a? to check if one object > is a same type or a subclass of another object. But what if i want to > check if one object is in some module? For example: > > module MyModule > class MyClass > end > end > > m = MyModule::MyClass.new > m.kind_of?(MyModule) # => false > > I would be happy if the statement above would return true since > MyClass is in module MyModule. Or why wouldn't it (except that it > hasn't meant to with current implementation)? > > Anyway, i was hoping to find some other method for this kind of > checks, but was unable to find any from Class, Module, Object, Kernel > classes/modules. So i made one example myself: http://gist.github.com/503646 The nesting of classes and modules is completely different from the ancestor/descendant relation, though. It's misleading to combine them. In your example, MyModule is not an ancestor of MyClass, but your descendant? method implies that it is. The ancestor relation (also the basis of kind_of?) is about the method lookup path. If this is true: obj.kind_of?(ModuleOrClass) that means that (unless other arrangements have been made) obj has access to the instance methods defined in ModuleOrClass. It also implies this: obj.class.ancestors.include?(ModuleOrClass) The question of where ModuleOrClass is defined, and how deeply nested it is, is a completely separate question. David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com