Initialize() in a module, is it correct?

Hi, for some exotic reason I need to define initialize() method in a
module, so a class including such module can invoke super() in its
initialize() method. It seems to work:

···

--------------------------------------------
module M
  def initialize
    puts "I'm module M"
  end
end

class A
  include M

  def initialize
    puts "I'm class A instance"
    super
  end
end

A.new
=> I'm class A instance
=> I'm module M
--------------------------------------------

However I would like to know if there could be an issue by using it. I
do know that constants defined in a module are not accessible from a
class including such module but this is not a problem.

PS: I don't want to inherit A from other class due to the nature of my code.

Thanks a lot.

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

Jesus.

···

On Mon, Jan 17, 2011 at 6:51 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Hi, for some exotic reason I need to define initialize() method in a
module, so a class including such module can invoke super() in its
initialize() method. It seems to work:

--------------------------------------------
module M
def initialize
puts "I'm module M"
end
end

class A
include M

def initialize
puts "I'm class A instance"
super
end
end

A.new
=> I'm class A instance
=> I'm module M
--------------------------------------------

However I would like to know if there could be an issue by using it.

This is what I expected (and hoped) :slight_smile:

Thanks a lot.

···

El día 17 de enero de 2011 19:08, Jesús Gabriel y Galán <jgabrielygalan@gmail.com> 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

--
Iñaki Baz Castillo
<ibc@aliax.net>

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

···

On Mon, Jan 17, 2011 at 7:32 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

El día 17 de enero de 2011 19:08, Jesús Gabriel y Galán > <jgabrielygalan@gmail.com> 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) :slight_smile:

Thanks a lot.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/