From: Jarmo Pertman on
Yup, regexp would be another possible way to do it.

But still, let me bring an example.

m = MyModule::AnotherModule::MyClass

if m.descendant?(MyModule)
# do something
else
# do something else
end

let's say that it's possible that m would be of type
MyModule::AnotherModule::MyClass,
MyModule::CompletelyAnotherModule::AnotherClass,
AnotherModule::YetAnotherClass.

Yes, it would make sense that if i knew which are all of the possible
types then i could do something like this instead:
if m.kind_of?(MyModule::AnotherModule::MyClass) || m.kind_of?
(MyModule::CompletelyAnotherModule::AnotherClass)
# do something
else
# do something else
end

but the problem is that i don't know all the possible types it might
be and it is decided at runtime. I just know that every object, which
is in module (for this example) MyModule should be handled in one way
and all objects which are not within MyModule namespace at all -
another way. So how would you recommend me to handle this kind of case
if you're saying that i should not do it? I still don't see any (much)
better solution than provided by me so far.

Jarmo Pertman
-----
IT does really matter - http://www.itreallymatters.net

On Aug 2, 3:30 pm, Brian Candler <b.cand...(a)pobox.com> wrote:
> => #<MyModule::AnotherModule::MyClass:0xb7436be4>>> o.class.name
>
> => "MyModule::AnotherModule::MyClass">> puts "Yippee" if o.class.name =~ /\bMyModule\b/
>
> Yippee
> => nil>> puts "Yippee" if o.class.name =~ /\bAnotherModule\b/
>
> Yippee
> => nil
>
> > 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?
>
> Modules are just namespaces. It seems odd to ask an object what
> namespace its class is in. As I've shown above, you can do this - but
> why would you want to?
>
> The only time I've wanted to do anything close to this is dynamic
> constant resolution:
>
> module X
>   class Foo
>     N=1
>     def show_n
>       puts self.class::N
>     end
>   end
>   class Bar < Foo
>     N=2
>   end
> end
> X::Foo.new.show_n   # 1
> X::Bar.new.show_n   # 2
>
> But even then, neither Foo nor Bar cares whether it is inside module X.
> You can remove the module X wrapper completely, changing X::Foo.new to
> Foo.new, and it will work just the same.
>
> Equally, if it *did* make a difference whether you were inside module X
> or module Y (perhaps because there were different constants or classes
> available), then you'd have to write your code differently within those
> modules anyway.
>
> module X
>   N=1
>   class Foo
>     def yo; puts N; end
>   end
> end
> module Y
>   M=2
>   class Bar
>     def yo; puts M; end
>   end
> end
> --
> Posted viahttp://www.ruby-forum.com/.