What is wrong with this super?


#1

Hello all, I am in the last exercise of OOP, I don't understand what is wrong with my super method. Can anyone please tell what is wrong here

class Message
@@messages_sent = 0
  def initialize(from, to)
     @from = from
     @to = to
     @@messages_sent += 1
  end
end

my_message = Message.new("Shravan", "Shaily")

class Email < Message
   def initialize
       super
   end
end

my_reply = Email.new() # When i pass 0 argument it says wrong number of arguments (0 for 2)
my_reply = Email.new("Shravan") # When i pass 0 argument it says wrong number of arguments (1 for 0)
my_reply = Email.new("Shravan", "Shaily") # When i pass 0 argument it says wrong number of arguments (2 for 0)

#2

In the code your provide, your Email class has the initialize method which takes 0 parameter. Therefore, when you'll use new, the method will expect 0 parameter given.

I think you're misusing the keyword super, because in your implementation it's going to look for the initialize of the Message class, which requires two parameters, but since the initialize from Email can't receive any, it's a bit pointless.
Regardless, since super is calling Message.initialize, it needs two parameters, so if you want to make your code work with Email.new() (the other two will not work), you can change super to super(42,43) (or any value you want).


#3

Note that you can also change the initialize method from the Email class to initialize(from, to), but in this case it's the first two statements (Email.new() and Email.new("Shravan")) which will not work.