initialize() in a module, is it correct?

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

Iñaki Baz Castillo

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
=3D> I'm class A instance
=3D> 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=
 
J

Jesús Gabriel y Galán

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
=A0def initialize
=A0 =A0puts "I'm module M"
=A0end
end

class A
=A0include M

=A0def initialize
=A0 =A0puts "I'm class A instance"
=A0 =A0super
=A0end
end

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

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
=3D> 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
=3D> nil
irb(main):019:0> A.new.test
test from A
test from M

Jesus.
 
I

Iñaki Baz Castillo

El d=C3=ADa 17 de enero de 2011 19:08, Jes=C3=BAs Gabriel y Gal=C3=A1n
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
=3D> 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
=3D> nil
irb(main):019:0> A.new.test
test from A
test from M

This is what I expected (and hoped) :)

Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<[email protected]>
 
R

Robert Klemme

El d=EDa 17 de enero de 2011 19:08, Jes=FAs Gabriel y Gal=E1n


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

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

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top