def initialize param1, param2
puts “#{param1} #{param2}”
end
end
If I call the second constructor, everything works fine, whereas I got
an ‘ArgumentError: wrong number of arguments (1 for 2)’ with the first.
So, has the second constructor overriden the first one? Does that mean
you have to implement ‘initialize’ only once in a class?
This is true for any method definition in Ruby, not just constructors.
There is no facility to select methods based on method signature, so
Ruby sees defining a method with the same name as an existing one as
replacing or overriding the original.
The answer to both of your questions is yes. Well, you can implement
it
twice, but, as in your code, the first one is irrelevant. Remember that
while defining a class, all you’re doing is executing code. Anyway,
things
like default values and the hash options idiom make method overloading
quite
unnecessary.