How can I prevent include one module multi-times?

Z

Zhao Yi

I am looking for a method to check whether this module has been
included. Does Ruby have this feature?
 
P

Peña, Botp

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

Thomas B.

Zhao said:
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.
 
J

James Coglan

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

2008/9/3 Thomas B. said:
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.
 
J

James Coglan

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

2008/9/3 James Coglan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top