cpppatrick
cpppatrick

Reputation: 769

Reading YAML file in Ruby

I apologize for the amateur question, I'm still learning. I'm trying to pull information from a YAML file in Ruby. I thought that because I had pushed the information to an array, all I would have to is print the array. I know that's not the case, but I couldn't find anything in the documentation when I looked.

require "yaml"

class BankAccount

attr_accessor :first_name, :last_name, :address, :your_account

def initialize
    @your_account = []
    open()
end

def open
    if File.exist?("accountinfo.yml")
    @your_account = YAML.load_file("accountinfo.yml")
    end
end

def save
    File.open("accountinfo.yml", "r+") do |file|
        file.write(your_account.to_yaml)
    end
end

def new_account(first_name, last_name, address)
    puts "Enter your first name:"
    first_name = gets.chomp
    puts "Enter your last name"
    last_name = gets.chomp
    puts "Enter your address:"
    address = gets.chomp
end

def account_review(your_account)
    puts @your_acccount
end

def run
    loop do
        puts "Welcome to the bank."
        puts "1. Create New Account"
        puts "2. Review Your Account Information"
        puts "3. Check Your Balance"
        puts "4. Exit"
        puts "Enter your choice:"
            input = gets.chomp
            case input
            when '1'
                new_account(first_name, last_name, address)
            when '2'
                account_review(your_account)
            when '4'
                save()
                break
            end
    end
end

end
bank_account = BankAccount.new
bank_account.run

Upvotes: 3

Views: 9809

Answers (3)

spickermann
spickermann

Reputation: 107077

You never actually set your variables. Prefix the setter with self., otherwise you would just create a local variable with the same name. Furthermore you don't set your your_acccount at all:

def new_account
  puts "Enter your first name:"
  self.first_name = gets.chomp
  puts "Enter your last name"
  self.last_name = gets.chomp
  puts "Enter your address:"
  self.address = gets.chomp

  self.your_account = [first_name, last_name, address]
end

Another issue will be that your code never calls open. That means everything works fine until you end program and restart it. Just call open before you call account_review to fix that.

Upvotes: 1

stef
stef

Reputation: 14268

When I'm facing something like this, I find it easiest to use irb to see what a YAML file looks like after it is loaded. Sometimes it can be in a format subtly different to what you were expecting.

In the same directory, on the command line, run irb.

You then have an interactive Ruby console where you can run commands.

require 'pp' - this helps you see output more easily.

Then:

your_account = YAML.load_file("accountinfo.yml")
pp your_account

In the code above, it appears that in your new_account method, you're not actually setting these attributes on @your_account, and in the save method you're writing an undefined variable to yaml.

Save should be:

file.write(@your_account.to_yaml) 

New account should end with:

@your_account[:first_name] = first_name
@your_account[:last_name] = last_name
@your_account[:address] = address

Upvotes: 4

Kabir Sarin
Kabir Sarin

Reputation: 18556

If you're using Rails, here's an easy way to read a YAML file (what the title says)

# Load the whatever_config from whatever.yml
whatever_config = YAML.load(ERB.new(File.read(Rails.root.join("config/whatever.yml"))).result)[Rails.env]

# Extract the foo variable out
foo = whatever_config['foo']

I don't think I really understand what your question is, exactly?

Upvotes: 1

Related Questions