[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How can I prevent include one module multi-times?

Zhao Yi

9/3/2008 2:59:00 AM

I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
--
Posted via http://www.ruby-....

8 Answers

Peña, Botp

9/3/2008 4:19:00 AM

0

RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haWwuY29tXSANCiMgSSBhbSBsb29r
aW5nIGZvciBhIG1ldGhvZCB0byBjaGVjayB3aGV0aGVyIHRoaXMgbW9kdWxlIGhhcyBiZWVuDQoj
IGluY2x1ZGVkLiBEb2VzIFJ1YnkgaGF2ZSB0aGlzIGZlYXR1cmU/DQoNCnlvdSBjYW4gcXVlcnkg
dGhlIGNsYXNzL21vZHVsZSB0aHJ1ICNpbmNsdWRlZF9tb2R1bGVzDQoNCmVnLA0KDQo+IGNsYXNz
IEMNCj4gZW5kDQo9PiBuaWwNCg0KPiBDLmluY2x1ZGVkX21vZHVsZXMNCj0+IFtLZXJuZWxdDQoN
Cj4gbW9kdWxlIE0NCj4gZW5kDQo9PiBuaWwNCg0KPiBjbGFzcyBDDQo+IGluY2x1ZGUgTQ0KPiBl
bmQNCj0+IEMNCg0KPiBDLmluY2x1ZGVkX21vZHVsZXMNCj0+IFtNLCBLZXJuZWxdDQoNCg0Kb3Ig
aWYgeW91IHdhbnQgdG8gcXVlcnkgdGhlIHdob2xlIHByb2dyYW0gc3BhY2UsIHNlYXJjaCB0aGUg
T2JqZWN0U3BhY2UNCg0KZWcsIA0KDQo+IE9iamVjdFNwYWNlLmVhY2hfb2JqZWN0KE1vZHVsZSku
c2VsZWN0e3xtfCBtLmNsYXNzLm5hbWUgPT0gIk1vZHVsZSJ9DQoNCj0+IFtJUkIsIEV4Y2VwdGlv
bjJNZXNzYWdlTWFwcGVyLCBNYXJzaGFsLCBPYmplY3RTcGFjZSwgR0MsIE1hdGgsIFByb2Nlc3M6
OlN5cywgUHJvY2Vzczo6R0lELCBQcm9jZXNzOjpVSUQsIFByb2Nlc3MsIFNpZ25hbCwgRmlsZTo6
Q29uc3RhbnRzLCBGaWxlVGVzdCwgRXJybm8sIFByZWNpc2lvbiwgRW51bWVyYWJsZSwgQ29tcGFy
YWJsZSwgS2VybmVsLCBSZWFkbGluZSwgUnVieVRva2VuLCBJUkI6Ok5vdGlmaWVyLCBNLCBJUkI6
Ok1ldGhvZEV4dGVuZGVyLCBJUkI6OkNvbnRleHRFeHRlbmRlciwgSVJCOjpFeHRlbmRDb21tYW5k
QnVuZGxlXQ0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==

Roger Pack

9/3/2008 5:34:00 AM

0

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-....

TPReal

9/3/2008 7:00:00 AM

0

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-....

James Coglan

9/3/2008 7:14:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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
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.

James Coglan

9/3/2008 7:15:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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
> 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.
>

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.

Sebastian Hungerecker

9/3/2008 8:29:00 AM

0

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

James Coglan

9/3/2008 8:35:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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:

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?

Roger Pack

9/4/2008 6:04:00 PM

0

> 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-....