is there anyone who has had experience with csv’s and ruby. i am new in it and am trying to make a program that uses a csv file to save names and passwords that are entered in by the user. i have read several other posts and threw in some of my own. but a bit of code that was left in had a grep method on the array i want to know if there is a replacement method that does something similar. now i need it to be able to search for the name in the csv then match the corresponding password and then check if the password matches if it does then you’ll gain access further.
def enter_the_vault(path_to_csv)
@names = []
csv_contents = CSV.read(path_to_csv)
csv_contents.shift
csv_contents.each do |row|
@names << row[0]
end
@names.each {|f| f.downcase! }
end
def search_for_user
puts "Please enter your username: "
usern = gets.chomp.downcase
results = [@names.grep(/#{usern}/)]
if results.each {|f| puts f}
prompt
else
puts "user does not exist"
prompt
end
end
def prompt
puts "Incorrect.. do you want to retry: (y/n)"
answer = gets.chomp.downcase
case answer
when /^y/
search_for_user
when /^n/
puts "Goodbye."
exit
else
prompt
end
end
This sounds curiously like a homework problem (usernames and clear-text passwords in a CSV?), but for working with CSV files I would suggest the FasterCSV library. It is an external dependency in Ruby 1.8 but included in 1.9.