I keep trying to reason upon the functionality of a method within a subclass that inherits functionality of a parents class. But it seems that I keep getting into a mental loop of: one cannot behave without the other but the other cannot come before the one... My brain hurts...
Ok heres my relevant code in the parent class
class BankAccount
# method to initialize and other methods etc...
def withdraw(amount)
if (amount <= @balance)
@balance -= amount
else
'Insufficient funds'
end
end
end
And heres my relevant code in the subclass
class CheckingAccount < BankAccount
# methods to initialize and other methods etc...
def withdraw
super
end
end
According to the tutorial im learning from - what I am trying to accomplish is
- "CheckingAccount methods #withdraw increments 'number_of_withdrawals' by one after a successful withdrawal"
So if I create a variable number_of_withdrawals
inside of my BankAccount
class (as tutorial examples hint towards) then how is it that when I call super
from the subclass version of withdraw
that it would know to increment number_of_withdrawals
based on the if
else
statement executing a withdraw or not.
Shouldn't a variable number_of_withdrawals
be declared in the BankAccount
class, not the CheckingAccount
class (even though tutorial examples hint towards putting it in the CheckingAccount
class). For a full picture of this here is a gist with the test specs() below of my current code state:
If someone can provide a working example of
- "CheckingAccount methods #withdraw increments 'number_of_withdrawals' by one after a successful withdrawal"
With modified code I have provided in the GIST - I would really appreciate it. Im very new to ruby.(1 week)