I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
···
--
Posted via http://www.ruby-forum.com/.
I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
--
Posted via http://www.ruby-forum.com/.
# I am looking for a method to check whether this module has been
# included. Does Ruby have this feature?
you can query the class/module thru #included_modules
eg,
class C
end
=> nil
C.included_modules
=> [Kernel]
module M
end
=> nil
class C
include M
end
=> C
C.included_modules
=> [M, Kernel]
or if you want to query the whole program space, search the ObjectSpace
eg,
ObjectSpace.each_object(Module).select{|m| m.class.name == "Module"}
=> [IRB, Exception2MessageMapper, Marshal, ObjectSpace, GC, Math, Process::Sys, Process::GID, Process::UID, Process, Signal, File::Constants, FileTest, Errno, Precision, Enumerable, Comparable, Kernel, Readline, RubyToken, IRB::Notifier, M, IRB::MethodExtender, IRB::ContextExtender, IRB::ExtendCommandBundle]
kind regards -botp
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
Zhao Yi wrote:
I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
Might try parsing Object.constants for the module name.
--
Posted via http://www.ruby-forum.com/\.
Zhao Yi wrote:
I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
What's your question? In the subject you ask 'How can I prevent include
one module multi-times?' and this is checked automatically without any
special handling.
--
Posted via http://www.ruby-forum.com/\.
That's partially correct, but you'll find that the module's included() hook
gets called repeatedly:
module M
def self.included(base)
puts "included M"
end
end
class C
# prints "included M" 3 times
include M
include M
include M
end
To check whether M is already mixed into C, the expression 'M > C' returns
true if C includes M. You could put this check inside M.included and throw
an exception if it's true.
2008/9/3 Thomas B. <tpreal@gmail.com>
Zhao Yi wrote:
> I am looking for a method to check whether this module has been
> included. Does Ruby have this feature?What's your question? In the subject you ask 'How can I prevent include
one module multi-times?' and this is checked automatically without any
special handling.
Roger Pack wrote:
Zhao Yi wrote:
> I am looking for a method to check whether this module has been
> included. Does Ruby have this feature?Might try parsing Object.constants for the module name.
Why? That only tells you whether that module exists (and it only tells you
that when the module is in the top-level namespace). It doesn't tell you
whether that module has been included into anything.
--
Jabber: sepp2k@jabber.org
ICQ: 205544826
I should have mentioned that including a module multiple times will not have
any nasty side effects on the inheritance tree of the including class --
this is checked for you.
2008/9/3 James Coglan <jcoglan@googlemail.com>
2008/9/3 Thomas B. <tpreal@gmail.com>
Zhao Yi wrote:
> I am looking for a method to check whether this module has been
> included. Does Ruby have this feature?What's your question? In the subject you ask 'How can I prevent include
one module multi-times?' and this is checked automatically without any
special handling.That's partially correct, but you'll find that the module's included() hook
gets called repeatedly:module M
def self.included(base)
puts "included M"
end
endclass C
# prints "included M" 3 times
include M
include M
include M
endTo check whether M is already mixed into C, the expression 'M > C' returns
true if C includes M. You could put this check inside M.included and throw
an exception if it's true.
Ah, I think I misunderstood the question. I wrote this a while ago to find
all the descendants of a module:
class Module
def descendants
classes =
ObjectSpace.each_object do |klass|
next unless Module === klass
classes << klass if self > klass
end
classes
end
end
So, a module has been included if
mod.descendants.size.nonzero?
2008/9/3 Sebastian Hungerecker <sepp2k@googlemail.com>
Roger Pack wrote:
> Zhao Yi wrote:
> > I am looking for a method to check whether this module has been
> > included. Does Ruby have this feature?
>
> Might try parsing Object.constants for the module name.Why? That only tells you whether that module exists (and it only tells you
that when the module is in the top-level namespace). It doesn't tell you
whether that module has been included into anything.
Ah, I think I misunderstood the question. I wrote this a while ago to
find
all the descendants of a module:
ah--if the problem is determining if a descendant exists, you could
write an included(instance) method within the included Module to track.
Cheers.
-=R
--
Posted via http://www.ruby-forum.com/\.