2016-06-23 4 views
0
@users = [ 
{id:"0", name:"Thomas One", email:"[email protected]", password:"adkdjaso"}, 
{id:"1", name:"James Five", email:"[email protected]", password:"jdajdlae"}, 
{id:"3", name:"Gordon Four", email:"[email protected]", 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

당신이

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

작품! 덕분에 같은 작업을 수행 할 수 있습니다 짝짓기 – AbNadi

관련 문제