From: Chuck Remes Date: 2008-07-04T23:54:00+09:00 Subject: Re: initialize and super with parameters On Jul 4, 2008, at 9:26 AM, Andy Joel wrote: > Some example code > > class MySuperClass > def initialize *args > puts "In SuperClass initialize" > end > end > > class MySubClass1 < MySuperClass > def initialize name > puts "In SubClass initialize: " + name > super > end > end > > 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 > parameters. > > 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 > parameter. Take a look at FAQ 5.9 here for your answer [1]. The call to "super" is not a real method call. When you call "super" without any params or parentheses, it starts the method lookup for the current method name but starting in the superclass while passing along all parameters (if any). If the superclass' method signature is different, you must explicitly tell super what to pass. If there are no parameters at all, empty parens () are sufficient. class A def initialize puts "superclass" end end class B < A def initialize(name) puts "my name is #{name}" super # <== wrong end end B.new("Chuck") This will throw an ArgumentError. Change the subclass like so: class B < A def initialize(name) puts "my name is #{name}" super() end end B.new("Chuck") All is well with the world. cr [1] http://www.math.ias.edu/doc/ruby-docs-1.8.2/faq-en/rubyfaq-5.html