From: Cees Zeelenberg Date: 2012-06-29T00:30:47+09:00 Subject: Interactions between 'new' and 'initialize' ? In JRuby, I am extending an existing Java Class with a Ruby initialising block by redefining the 'new' class method. This works fine except for new classes which inherit from the original class and have a different number of arguments. These classes generate a 'wrong number of arguments' error. The behavior seems not specific for Java Classes and is actually the same for Ruby and JRuby. The following example illustrates it, the first version works as expected class Parent def initialize(y) puts "I am parent #{y}" end end class Child < Parent def initialize(c1,c2) puts "I am a child #{c1} #{c2}" end end Parent.new('A') # => I am parent A Child.new('B','C') # => I am a child B C The next example adds a redefinition of Parent.new; Parent goes on to work as expected, but Child fails: class Parent class << self def new(arg) puts "redefining new for parent" super(arg) end end def initialize(y) puts "I am parent #{y}" end end class Child < Parent def initialize(c1,c2) puts "I am a child #{c1} #{c2}" end end Parent.new('A') # => redefining new for Parent # => I am parent A Child.new('B','C') # => ArgumentError: wrong number of arguments (2 for 1) Is this behavior intentional or a bug? At least it is consistent (Ruby 1.8.7, 1.9.1 JRuby 1.6.7) I have tried using alias instead of super and changing the order of redefining new and defining initialize. I also tried using class_eval and taking the Person.new definition out of the original class definition. All to no avail. Any help would be appreciated. -- Posted via http://www.ruby-forum.com/.