Have A Little Loop In Your Life…

Rumiko Acopa
3 min readJul 4, 2021

I built a basic authenticator 1.0 app with ruby. I was able to go over concepts like arrays, hashes and while loop. Here I have an array and each element in the array has a hash. The hash contains two elements in it a username key with the value and password value. The username and password are symbols and the values are strings.

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' }]

I keep hearing that you need to come up with a plan. Planning is not always my forté but I’m working on it!

The goal is to work through execution flow, handle errors and extracting logic to a method. Once we have a password we want to loop through each user. Compare & authenticate the username that is entered will match the username and the same with the password.

Initially what it may have looked like before creating a method to handle the username and password is the code below. What would be preferred is If found then we want to exit the loop. This is very sloppy and that is why we need to create a method to call later.

The code below will give you an error, it’ll match the first one and continue on trying to match all the other ones. So, you will get “credentials not correct”.

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

We can create a method to handle the authentication like this below:

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

This is the method that we can call later and it makes the code so much cleaner! Yay!

Photo by Pineapple Supply Co. on Unsplash

Prompting the user for an input at each loop and if it’s the letter ’n’ then it will keep looping. The While Loop keeps working until the user presses ’n’. While the condition is true it will keep running/looping until it hits a ‘break’ or end condition.

If you happen to get stuck in an Infinite Loop then press CTRL C to exit out of it.

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

Definitely basic material here but hey you got to start somewhere. Udemy.com is great. I like their courses and price is great. This app was a codealong and it definitely helped me to feel a bit more confident since I could understand what they were discussing.

Happy Coding!

#CodingEsty

--

--