From: Jarmo Pertman Date: 2010-08-02T20:35:17+09:00 Subject: Re: how to check if some object is included in some module? Hello! But the examples provided by you won't work since #constants return only immediate constants and not constants recursively. Thus: irb(main):003:0> module MyModule irb(main):004:1> module AnotherModule irb(main):005:2> class MyClass irb(main):006:3> end irb(main):007:2> irb(main):008:2* class AnotherClass irb(main):009:3> end irb(main):010:2> end irb(main):011:1> end => nil irb(main):012:0> MyModule.constants => ["AnotherModule"] John, what do you even mean by "not to modify Object#descendant?". Object doesn't have any methods named like that - it was just method defined by me. Name of the method didn't have so much importance for me, but the solving of the problem itself. So, both of you think that this solution would be best to do something like this? Or you're saying that i should not do something like this at all? Again, ignore the name of the method itself. Jarmo Pertman ----- IT does really matter - http://www.itreallymatters.net On Aug 2, 1:48 am, John Croisant wrote: > On Sun, Aug 1, 2010 at 2:20 PM, 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 > > > I still think that there should be some easier built-in way to perform > > this kind of checks. Maybe i have just missed it... > > > Jarmo Pertman > > I would do it this way: > >   MyModule.constants.include?(:MyClass) # note the ":", it's a symbol > > (It's also possible to define a general method which takes a class, > gets the class's name, converts it to a symbol, and then tests whether > the module has that constant. But I'll leave that an an excercise for > the reader.) > > Another possible way to do it would be this: > >   MyModule.const_get(:MyClass) > > This will return MyModule::MyClass if it exists (which will be > considered true in an "if" statement), or raise NameError if it > doesn't exist. Because it raises an error in the negative case, it's > not convenient to use it in an "if" statement, which is why I consider > it less desirable than the first way. > > HTH, > > - John > > P.S. I second David's advice not to modify Object#descendant?. The > relationship you are testing for is not ancestry/inheritance, so it > doesn't make much sense for the #descendant? method to do it.