Hi,
I am quite new to Ruby and make some experiences with modules and
classes.
I have defined a module and an inner class with the same name (Cucc) and
a static method (x) for the class. Then I made a redefinition (or
whatever you call) of the same module (Cucc) without the inner class but
with a static (x) method.
Before the redefinition x method of the Cucc module’s inner class Cucc
is called but right after the modules static method is called. Why?
I totally agree that coding style like below is among the first 10 on
the list to avoid during programming, but though I want to understand
the behavior!
I have created the following code:
------CODE START-----------
module Cucc
def Cucc.x(arg)
puts arg.to_s + " Cucc.x"
end
def x(arg)
puts arg.to_s + " Cucc module x"
end
class Cucc
def Cucc.x(arg)
puts arg.to_s + " Cucc module Cucc class Cucc.x"
end
def x(arg)
puts arg.to_s + " Cucc module Cucc class x"
end
end
end
puts “Cucc class started first”
Cucc::x(10)
c = Cucc::Cucc.new
c.x(20)
Cucc::Cucc.x(30)
puts “Cucc class ended first”
module Cucc
def Cucc.x(arg)
puts arg.to_s + " Redefined Cucc module Cucc.x"
end
def x(arg)
puts arg.to_s + " Redefined Cucc module x"
end
end
puts “Cucc class started second”
Cucc::x(10)
c = Cucc::Cucc.new
c.x(20)
Cucc::Cucc.x(30)
puts “Cucc class ended second”
------CODE END-----------
The outcome is the following:
Cucc class started first
10 Cucc.x
20 Cucc module Cucc class x
30 Cucc module Cucc class Cucc.x
Cucc class ended first
Cucc class started second
10 Cucc.x
20 Cucc module Cucc class x
30 Redefined Cucc module Cucc.x
Cucc class ended second
How the hell is it possible that for the “30” that I get different
result when having a redefinition (or so) of the Cucc module?
At first the inner class’ static method is called and in the second case
the static method of the module!
Thanks in advanced, --stan–