How to exec a module method when "include Module" from a Class ?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I have a class C that includes a module M.
I need that when a C object is created a module M method is runned=20
automatically (without call it from "initialize" method) to fill an object=
=20
attribute. Something as:

=2D--------------------------------------
module M
def module_method
@words << "module_word"
end
end

class C
include C

attr_reader :words

def initialize
@words =3D []
@words << "class_word"
end
end
=2D--------------------------------------

irb> c=3DC.new
irb> c.words
["class_word", "module_word"]


Is it possible without adding "module_method()" in class C initialize metho=
d?

Thanks a lot.



=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

MjAwOC81LzEsIEnDsWFraSBCYXogQ2FzdGlsbG8gPGliY0BhbGlheC5uZXQ+OgoKPiAgaXJiPiBj
PUMubmV3Cj4gIGlyYj4gYy53b3Jkcwo+ICBbImNsYXNzX3dvcmQiLCAibW9kdWxlX3dvcmQiXQo+
Cj4KPiAgSXMgaXQgcG9zc2libGUgd2l0aG91dCBhZGRpbmcgIm1vZHVsZV9tZXRob2QoKSIgaW4g
Y2xhc3MgQyBpbml0aWFsaXplIG1ldGhvZD8KCgpPZiBjb3Vyc2UgSSBjb3VsZCB1c2UgaW5oZXJp
dGFuY2UgYW5kIHN1cGVyIG1ldGhvZCBpbiAiaW5pdGlhbGl6ZSgpIgpidXQgaW4gZmFjdCBJIG5l
ZWQgdG8gaW5jbHVkZSBtb3JlIHRoYW4gYSBtb2R1bGUgc28gaW5oZXJpdGFuY2UKKHNpbmdsZSB1
c2luZyAiY2xhc3MgPCBjbGFzcyIpIGlzIG5vdCB2YWxpZCBmb3IgbWUuCgoKLS0gCknDsWFraSBC
YXogQ2FzdGlsbG8KPGliY0BhbGlheC5uZXQ+Cg==
 
S

Sandro Paganotti

You can try overwrite initialize method from the module

module M

def self.included(classname)
classname.class_eval <<-EOS
alias_method :eek:ld_initialize, :initialize
def initialize
old_initialize
module_method
end
EOS
end


def module_method
@words << "module_word"
end
end

class C

attr_reader :words

def initialize
@words =3D []
@words << "class_word"
end

include M
end

Sandro
www.railsonwave.com


irb> c=3DC.new
irb> c.words
["class_word", "module_word"]


Is it possible without adding "module_method()" in class C initialize=
method?


Of course I could use inheritance and super method in "initialize()"
but in fact I need to include more than a module so inheritance
(single using "class < class") is not valid for me.



--=20
Go outside! The graphics are amazing!
 
I

Iñaki Baz Castillo

MjAwOC81LzIsIFNhbmRybyBQYWdhbm90dGkgPHNhbmRyby5wYWdhbm90dGlAZ21haWwuY29tPjoK
PiBZb3UgY2FuIHRyeSBvdmVyd3JpdGUgaW5pdGlhbGl6ZSBtZXRob2QgZnJvbSB0aGUgbW9kdWxl
Cj4KPiAgbW9kdWxlIE0KPgo+ICAgICAgICBkZWYgc2VsZi5pbmNsdWRlZChjbGFzc25hbWUpCj4g
ICAgICAgIGNsYXNzbmFtZS5jbGFzc19ldmFsIDw8LUVPUwo+ICAgICAgICBhbGlhc19tZXRob2Qg
Om9sZF9pbml0aWFsaXplLCA6aW5pdGlhbGl6ZQo+ICAgICAgICAgIGRlZiBpbml0aWFsaXplCj4g
ICAgICAgICAgICBvbGRfaW5pdGlhbGl6ZQo+ICAgICAgICAgICAgbW9kdWxlX21ldGhvZAo+ICAg
ICAgICAgIGVuZAo+ICAgICAgICBFT1MKPiAgICAgICAgZW5kCj4KPgo+Cj4gICAgICAgIGRlZiBt
b2R1bGVfbWV0aG9kCj4gICAgICAgICAgICAgICAgQHdvcmRzIDw8ICJtb2R1bGVfd29yZCIKPiAg
ICAgICAgZW5kCj4gIGVuZAo+Cj4gIGNsYXNzIEMKPgo+Cj4gICAgICAgIGF0dHJfcmVhZGVyIDp3
b3Jkcwo+Cj4gICAgICAgIGRlZiBpbml0aWFsaXplCj4gICAgICAgICAgICAgICAgQHdvcmRzID0g
W10KPiAgICAgICAgICAgICAgICBAd29yZHMgPDwgImNsYXNzX3dvcmQiCj4gICAgICAgIGVuZAo+
Cj4KPiAgICAgICAgaW5jbHVkZSBNCj4gIGVuZAoKCkdyZWF0ICEhIQpJIGRpZG4ndCBrbm93IHRo
ZSBtZWFuaW5nIG9mICJpbmNsdWRlZCIgY2xhc3MgbWV0aG9kIG9mIE1vZHVsZS4gVmVyeQp1c2Vm
dWwgaW4gY29uanVuY3Rpb24gd2l0aCAiY2xhc3NfZXZhbCIKOikKClRoYW5rcyBhIGxvdC4KCgoK
LS0gCknDsWFraSBCYXogQ2FzdGlsbG8KPGliY0BhbGlheC5uZXQ+Cg==
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top