From: GOTO Kentaro Date: 2002-08-08T05:05:09+09:00 Subject: Re: Inherited question At Thu, 8 Aug 2002 04:17:17 +0900, Alan Chen wrote: > I'm playing around with the inherited hook and I'm having some problems. > The following code is running on "ruby 1.6.7 (2002-03-19) [i386-linux]". > It works fine when I leave the inherited def out, and explicitly register > the child class. However, if I attempt the same with the hook, I get the > following error: > > check-inherited.rb:9:in `initialize': wrong # of arguments(0 for 1) (ArgumentError) > from check-inherited.rb:9:in `new' > from check-inherited.rb:9:in `register' > from check-inherited.rb:5:in `inherited' > from check-inherited.rb:20 > ### check-inherited.rb > > class Base > @@childitems = {} > > # def self.inherited(subclass) > # self.register(subclass) > # end > > def self.register(subclass) > item = subclass.new > @@childitems[item.name] = item > end > > attr_reader :name > > def initialize(name) > @name = name > end > end > > class Sub1 < Base > def initialize > super('child_sub1') > end > end > > # not called when testing with self.inherited commented-in > Base.register(Sub1) > > ### end of file Class#inherited would be called when the line "class Sub1 < Base" is evaluated. In the time, Base#initialize is not defined yet and the arity (i.e. # of args) of Sub1.new is 1, which is same to Base.new's because the arity of Base#initialize is 1. The following sketches the problem. class A def initialize(x) end end class B < A end # p B.new # uncomment and will get ArgumentError class B def initialize() end end p B.new -- Gotoken