Have A Little Loop In Your Life…

Start with an Array of Users

users = [{ username: 'Miranda', password: 'password1' },{ username: 'Scott', password: 'password2' },{ username: 'Angela', password: 'password3' },{ username: 'Flora', password: 'password4' },{ username: 'Juan', password: 'password5' }]
attempts = 1while attempts < 4
print "Username: "
username = gets.chomp
print "Password: "
password = gets.chomp
users.each do |user|
if user[:username] == username && user[:password] == password
puts user
else
puts "Credentials were incorrect"
end
end
puts "Press n to quit or any other key to continue: "
input = gets.chomp
break if input == "n"
attempts += 1
def auth_user(username, password, list_of_users)list_of_users.each do |user_record| #looping through comparingif user_record[:username] = username && user_record[:password] ==        passwordreturn user_record
end
end
return "Credentials were not correct"end#getting user object returns the user and exits the method.#last return is implied so technically don't need to put 'return' before credentials phrase
Photo by Pineapple Supply Co. on Unsplash
puts "Bienvenidos to the authenticator 1.0"25.times {print "-"}attempts = 1while attempts < 4print "username: "username = gets.chompprint "password: "password = gets.chompauthentication = auth_user(username, password, users)puts authenticationputs "Press n to quit or any other key to continue: "input = gets.chomp.downcase #get input from user at each loop, if input is the letter 'n' then it will quit otherwise it will keep looping.break if input == 'n'attempts += 1endputs "You have exceeded the number of attempts" if attempts == 4

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store