I noticed some very strange behaviour in a class where I was accidentally calling super on a class with no superclass. Obviously I shouldn’t have been calling super, but I found the Errors very odd:
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 Argument error occur and why doesn’t a more specific error related to calling super on a non-existant superclass occur?
SomeClassimplicitly extendsObjectandObjecthas an implicit no-argsinitializemethod.Using
superbare (i.e. with no args or parens) sends the superclass the same message as was received by the subclass. In your example, usingsuperinSomeClass#initialize(arg)is actually sending#initialize(arg)to Object – hence the error.The reason that there isn’t a more specific error is that it isn’t a special circumstance.