How would I access a key from a hash that is within an array that is within a text file in ruby? -


i'm trying create if/else statement compares user's input against name keys within hash that's stored text file. how write this?

user_accts array.

update of full if/else statement:

elsif choice == "2" puts "====== new customer ======" puts "choose username:" prompt; login_name = gets.chomp

    #file.open("cust_accts.txt", "r")      #if @user_accts.map { |acct| acct["name"]}.include?(login_name)     if @user_accts.any? {|acct| acct["name"] == login_name }         puts "sorry, username has been taken"      elsif            puts "choose password:"         prompt; login_password = gets.chomp          user_accts << create_account(login_name, login_password)         file.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}                 end      

original if/else statement:

if login_name == #??? @user_accts.has_key?(login_name) ???             puts "sorry, username has been taken"          elsif                puts "choose password:"             prompt; login_password = gets.chomp              user_accts << create_account(login_name, login_password)             file.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}                     end  

this inputted cust_accts.txt file using command:

user_accts << create_account(login_name, login_password)

file.open("cust_accts.txt", "a") { |file| file.puts(user_accts)}

cust_accts.txt

{"name"=>"tom", "password"=>"popcorn", "balance"=>0} {"name"=>"avril", "password"=>"chain", "balance"=>0} 

it's not quite clear starting point is.

assuming have parsed text file @user_accts, have:

@user_accts = [{"name"=>"tom", "password"=>"popcorn", "balance"=>0},                {"name"=>"avril", "password"=>"chain", "balance"=>0}] 

then want do:

if @user_accts.map {|acct| acct["name"]}.include?(login_name)   puts "sorry, username has been taken" else   # ... end 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -