Wednesday, 23 November 2016

4 Initializing instances

Using initialize methods can make objects usage in safer manner..
class Account
        def ac_deposit(d)
                @balance=10000
                @balance=@balance+d
        end
        def withdraw(a)
                @balance-=a
        end

        def show_bal
                puts "\tCurrent balance: #{@balance}"
        end
end

currentAC=Account.new
amt=200
puts "\ndeposit #{amt}"
currentAC.ac_deposit(amt)
currentAC.show_bal

amt=2000
puts "\nWithdraw #{amt}"
currentAC.withdraw(amt)
currentAC.show_bal
In Ruby Class object instance

No comments:

Post a Comment