From: Robert Klemme Date: 2011-01-18T18:36:08+09:00 Subject: Re: initialize() in a module, is it correct? On Mon, Jan 17, 2011 at 7:32 PM, Iñaki Baz Castillo wrote: > El día 17 de enero de 2011 19:08, Jesús Gabriel y Galán > escribió: >>> However I would like to know if there could be an issue by using it. >> >> I don't think so. initialize it's just an instance method, just like >> any other, with the same lookup path: >> >> irb(main):001:0> module M >> irb(main):002:1> def test; puts "test from M"; end >> irb(main):003:1> end >> => nil >> irb(main):015:0> class A >> irb(main):016:1> include M >> irb(main):017:1> def test; puts "test from A"; super; end >> irb(main):018:1> end >> => nil >> irb(main):019:0> A.new.test >> test from A >> test from M > > This is what I expected (and hoped) :) > > Thanks a lot. But you should change the signature to make it more compatible with arbitrary inheritance chains: module M def initialize(*a,&b) super puts "I'm module M" end end Now you can use this in different inheritance chains: class X def initialize puts "X" end end class Y < X include M def initialize super puts "Y" end end class A def initialize(x) printf "A(%p)\n", x end end class B < A include M def initialize(a, b) super(a) printf "A(%p,%p)\n", a, b end end B.new 1,2 Y.new Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/