2017-12-08 9 views
0

Ruby를 사용하여 코드에 문제가 있습니다. 그것의 단지 터미널 프로그램을위한, 그래서 웹 사이트 또는 아무것도.루비 | 파일과 비교하여 암호를 확인하려고 시도했습니다.

내 코드에서 사용자가 로그인을 만들도록합니다. 그런 다음 로그인 할 사용자를 갖지만 암호 나 사용자 이름이 올바른지 확인하는 방법을 알아낼 수 있습니다.

프로그램은 사용자가 사용자 이름/암호로 입력 한 내용과 파일 (userdatabase)을 비교해야합니다.

이제 while 루프를 사용하여 입력을 사용자 데이터베이스에서 찾을 수 없으면 사용자를 중지하려고합니다.하지만 그 작업을 수행 할 수없는 것 같습니다.

코드 :

puts "What will be your user name?" 

username = gets.chomp

puts "What will be your password?" 

password = gets.chomp 

puts "Please repeat your password." 

passwordsafe = gets.chomp 



f = File.new("student.txt", "w+") 
f.puts username + ";" + password + ";" + passwordsafe 
f.close 

puts "well done, you have created a new user." 

lines = IO.readlines("student.txt") 
lines.each{|line| print(line)} 


puts "now you need to login." 
puts "What is your username?" 
username = gets.chomp 

File.open("student.txt") do |f| 
    f.any? do |line| 
    while line.include?(username) 
    end 
end 

elsif puts "Sorry your username was incorrect" 
end 

#lines = IO.readlines("student.txt") 
#lines.each{|line| (line)} 

puts "what is your password?" 
password = gets.chomp 
+2

을, 구문 오류의 나머지 부분에 관해서는

, 내가 당신에게 그를 떠날거야. co.kr/codahale/bcrypt-ruby). – tadman

답변

0

while는 불필요합니다.

사용자 이름 확인은 현재이 좋아 :

File.open("student.txt") do |f| 
    f.any? do |line| 
    while line.include?(username) 
    end 
end 

이 같을 수 :

File.open("student.txt").each_line.any? do |line| 
    line.include?(username) 
end 

또는 같은 한 줄에 붕괴 :

File.open("student.txt").each_line.any?{ |l| l.include?(username) } 

이 문서를에 확인 any? 메서드를 사용하여 예상되는 내용을 이해할 수 있습니다. http://ruby-doc.org/core-2.4.2/Enumerable.html#method-i-any-3F // GitHub의 : 당신이 암호에 대해 학습하는 경우는 [안전한 암호 해시 (HTTPS에 대해 배울 수있는 시간)

+0

오, 그래서 'elsif'지금은 잘 작동하는 것 제거했습니다. 하지만 내 문제는 내가 돌아가서 데이터베이스에없는 경우 사용자 이름을 다시 묻기를 원한다는 것입니다. 어떻게해야합니까? – Learner