搜索一系列哈希并打印出结果

时间:2016-06-23 22:02:47

标签: ruby

@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

如何根据用户的电子邮件搜索一系列哈希值,然后打印出他的名字?

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

@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