0

Teaching myself Ruby so please bear with me. If I create an object with several defining attributes and push this object into an array how do I access one of those attributes in another method to use it in a control flow scheme? I'm making a banking ATM program for fun. My code is below...

class Bank


    class AccountMaker
        attr_accessor :account_number, :name, :balance, :pin

        def initialize(account_number, name, balance, pin)
            @account_number = account_number
            @name = name
            @balance = balance
            @pin = pin
        end
    end

    def initialize
        @accounts = []
    end

    def add_account(account_number, name, balance, pin)
        account = AccountMaker.new(account_number, name, balance, pin)
        @accounts << account
    end

    def login_screen(accounts)

        def account_number_login(accounts)
            puts "Please enter your 7 digit account number."
            account_number_input = gets.chomp 
            puts accounts.instance_variable_get(:account_number)

            if (/^\d{7}$/ === account_number_input) and (account_number_input === (what should go here) )
                thank_you_msg()
                pin_login(account_number_input)
            else 
                error_msg()
                account_number_login()
            end
        end

I have more code after this but its not pertinent to the question. Essentially I want to extract from the accounts array :account_number and use it in the if statement within the Login_screen function to see if the account actually exists. Any and all help would be appreciated.

1 Answer 1

2

accounts is an array. So you have to access one of its elements' account_number instance variable. For example the first element's:

# accounts[0] would return an instance of `AccountMaker `
accounts[0].instance_variable_get(:account_number)

Also, you don't need to use instance_variable_get, since you already declared it as accessor. So, you can just call account_number method on it.

accounts[0].account_number
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome thanks. Now what if I wanted to match it up against objects? Say I have several different objects with diff it account numbers and I wanted the one object account that matched with what account number the user inputs?
You would probably want to use Hash instead. The account number would be a key and the actual account the value.
I thinking that as well. I went ahead made it a hash, so same question as before only how do I access the Hash within the array, specifically the :account_number value?
No, I meant throw away your array and use hash instead. @accounts[account_number] = AccountMaker.new(account_number, name, balance, pin). When you want to check if there is an account with this account number: if(@accounts[account_number_input]) ... else ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.