AbNadi AbNadi - 5 months ago 6
Ruby Question

search through an array of hashes

@users = [
{id:"0", name:"Thomas One", email:"thomas@fake.nl", password:"adkdjaso"},
{id:"1", name:"James Five", email:"j4@fake.nl", password:"jdajdlae"},
{id:"3", name:"Gordon Four", email:"g4@fake.nl", password:"adsldsae"}
]

def getId
puts "what is your email address?"
@account = gets.chomp

if @users.find |x| x[email:] == @account
puts "Welcome #{@users[name:]}"
break
else
puts "your account does not exist try again!"
end
end


how do i search through an array of hashes based of the users email and then print out his name ?

Answer

You can do something like this

@email = "thomas@fake.nl"
user = @users.find {|u| u[:email] == @email}
if user
  puts "Welcome #{user[:name]}"
else
  puts "Your account does not exist try again!"
end
Comments