Attempting to learn Ruby using the new LearnStreet online tutorials.
Have attempted to get help via their Q&A system but it seems nobody answers them.
"Can you now implement the withdraw! method on account object, which takes one parameter amount and reduces the balance by the specified amount? After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."
Is the question and I got the two hints of
" Hint 1 The code @balance = @balance - amount reduces the amount from @balance.
Hint 2 Then call the method withdraw! on the account object - account.withdraw!(100). "
My attempt was
def
account.widthdraw!
@balance = @balance - amount
end
account.withdraw!(100)
Any ideas what I'm missing?
"Can you now implement the withdraw! method on account object, which takes one parameter amount and reduces the balance by the specified amount? After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."
One step at a time:
"Can you now implement the withdraw! method on account object
class Account
def withdraw!
end
end
which takes one parameter amount...
class Account
def withdraw!(amount)
end
end
and reduces the balance by the specified amount?
class Account
def withdraw!(amount)
@balance = @balance - amount
end
end
After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."
account = Account.new
account.withdraw!(100)
I think you'd want something like this.
class Account
def withdraw! amount
@balance -= amount
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With