Banking on Ruby


Hi all, I'm trying to create a new method in the banking example that allows for a transfer. Below are the relevant details:

class Account
    attr_reader :name
    attr_reader :checking_balance
    attr_reader :savings_balance
    def initialize(name, checking_balance = 100, savings_balance)
        @name = name
        @checking_balance = checking_balance
        @savings_balance = savings_balance
        @total_balance = @checking_balance + @savings_balance

def transfer(pin_number,amount)
        if pin_number != pin
            puts "Which account would you like to transfer from? [checking/savings]?"
            x = gets.chomp
            case x
                when "checking"
                other = "savings"
                from = @checking_balance
                to = @savings_balance
                when "savings"
                other = "checking"
                from = @savings_balance
                to = @checking_balance
                puts "I'm sorry, I don't recognize that account name."
            if amount > from
            puts "I'm sorry, you do not have sufficient funds to make this transfer at this time. Please deposit funds or choose a different transfer amount."
            from -= amount
            to += amount
            puts "You transferred $#{amount} from your #{x} account to your #{other} account. Your new #{x} account balance is $#{from}. Your new #{other} account balance is $#{to}. Thank you for your transaction."

    def pin 
        @pin = 1234 
    def pin_error 
        return "Access denied: incorrect PIN." 

    checking_account ="Eric",400,1000)

When I run the code, I am seeing the initial output "Which account would you like to transfer from? [checking/savings]?" The problem is that I am getting the error "The program took too long to finish. Check your code for infinite loops and try again." without ever being able to provide any input via the gets.chomp line. There are no loops here, so I don't know why my code would be infinite looping. When I remove the gets.chomp line and include the "from" account in my parameters for the method, the entire thing runs fine. It seems that the issue has to do with this gets line, but I can't figure it out.

I'd appreciate any help I can get.


Hey there, I added a balance method to check the balance.. seems to be working fine.