I am a newbie to Ruby and the output of the following program is not
clear
to me.
The program is:
class Accounts
attr_accessor :balance
def initialize(name, phone, op_balance)
@name = name
@phone = phone
@balance = op_balance
end
def deposit(amt)
@balance += amt
puts @balance.to_s
end
end
class Savings<Accounts
def initialize(name, phone, op_balance)
super
end
def withdraw(amt)
@bal = balance
if ((@bal - amt) > 500.00)
balance = @bal - amt
puts balance.to_s
else
puts ‘No Balance’
end
end
end
s = Savings.new(‘Pete’,65241884,5000.00)
s.deposit(500.00)
s.withdraw(200.00)
puts s.balance.to_s
The output I get is:
ruby accounts.rb
5500.0
5300.0
5500.0
Exit code: 0
I thought that the last line puts s.balance.to_s would have displayed
5300.0.
Why is this 5500.0 ?
All help appreciated.