Unusual constructor behavior

I noticed very strange behavior in a class where I accidentally called super in a class without a super class. Obviously, I would not have to call super, but I found the Errors very strange:

class SomeClass def initialize(someparam) super end end 

Then:

 SomeClass.new() # ArgumentError: wrong number of arguments (0 for 1) SomeClass.new('cow') # ArgumentError: wrong number of arguments (1 for 0) 

Why does the second error Argument occur and why does not a more specific error arise related to calling super in a nonexistent superclass?

+4
source share
1 answer

SomeClass implicitly extends Object , and Object has an implicit no-args initialize method.

Using super bare (i.e., without arguments or guys) sends the superclass the same message as the subclass received. In your example, using super in SomeClass#initialize(arg) actually sends #initialize(arg) to Object - hence the error.

The reason why there is no more specific error is because this is not a special circumstance.

+4
source

All Articles