Ryan Glass Ryan Glass - 21 days ago 4
Ruby Question

Subclass accessing the parent in ruby

I'm having a little problem understanding here, I'm trying to get the subclass to access the parent class. Which I believe is class b < a. But after I run the program, it tells me that I have not defined the checking class. Doesn't inheritance define the class? Would I use another def initialize under the checking class with a super to define it? If so what would I initialize?

class Account
attr_accessor :number, :principal, :type

def initialize(initial_deposit)
@number = Bank.size + 1
@principal = initial_deposit.to_f
end

def deposit(amt)
@principal += amt
self.balance
printf "$%.2f successfully deposited\n", amt
printf "Current balance is $%.2f\n", @principal
end

def withdraw(amt)
@principal -= amt
if @principal < 0
puts "Your #{@type} account #{@number} is overdrawn!"
printf "Current account balance: -$%.2f\n", @principal.abs
printf "Please deposit at least $%.2f immediately to avoid additional fees!\n", @principal.abs
else
self.balance
printf "%.2f successfully withdrawn. Current balance is %.2f\n", amt, @principal
end
end
end

class Checking < Account

def
super
end

def balance()
@balance = principal * (1 + interest_rate / 365) ** 365
end

end

Answer

You missed an initialize. Change:

class Checking < Account
  def 
     super
  end

  def balance()
    @balance = principal * (1 + interest_rate / 365) ** 365
  end
end

to

class Checking < Account
  def initialize
     super
  end

  def balance
    @balance = principal * (1 + interest_rate / 365) ** 365
  end
end

And your next issue will be that Checking#new (initialize) doesn't take parameters, but you call super and Account#new expects one argument.

Comments