Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong number of arguments when calling super

Tags:

ruby

class A
  def initialize
    print "Hello! "
  end
end

class B <  A
  def initialize(name)
    super
    print "My name is #{name}!"
  end
end

test = B.new("Fred")

And I get

wrong number of arguments (1 for 0)

But why? Class B requires one argument, and I am giving it all right. Class A doesn't require any argument, so I am not passing anything through super at all.

like image 716
Voldemort Avatar asked Aug 02 '12 20:08

Voldemort


1 Answers

You need to use super() in order to call it with no arguments. Super by itself automatically calls the parent with the arguments provided to itself (ie. "Name")

like image 190
Calvin Jia Avatar answered Oct 24 '22 07:10

Calvin Jia