Some example code
def initialize *args
puts “In SuperClass initialize”
class MySubClass1 < MySuperClass
def initialize name
puts "In SubClass initialize: " + name
The super in the subclass will take with it any parameters that were
sent to the initialize method, so this will throw an error as the
initialize in MySuperClass does not accept the same number of
This is surprisingly restrictive (given the nature of Ruby generally).
If you are extending a class with a parameterless initializer, you
cannot access the superclass initializer if you want to include a
The initialize method is automatically private (and appears to be stuck
like that), so cannot be invoked through super.initialize or something
Three possible workarounds: When you create an initialize method, always
have a parameter with a default value, even if you just discard it. This
will allow subclasses to have zero or one parameters, and effectively
any number through a hash. A better way perhaps is to use *args as the
parameter list, even if you do not use it. If you cannot modify the
superclass (say it is in someone else’s API), the nly alternatiove I can
think of is to initialise with another method, say init, but then you
are relying on any API user remembering to do that - the whole point of
initialize is so they (and you) do not have to remember.
These are all hacks. Is there a better way? Or have I missed something?