我是一個初學者,有一些基本的課程。Ruby 1.9.3如果已經在數據庫中創建新用戶/密碼並重新提示?
試圖創建一個新用戶,如果該用戶已經在哈希中,再次提示用戶,但是一旦用戶爲零,請求輸入密碼,然後添加到哈希中。我做了很多嘗試。這只是其中之一。
users = {
user_1: 2
}
puts "Create New User: Type your username"
new_user = gets.chomp
if users[new_user.to_sym].nil?
puts "Type in a Password"
pass = gets.chomp
users[new_user.to_sym] = pass.to_i
elsif users[new_user.to_sym] != nil
puts "We already have that user."
loop do
if users[new_user.to_sym] != nil
puts "Try Again"
ta = gets.chomp
break if users[ta.to_sym] == nil
end
end
end
我一直在使用"when, then", "if/else"
,"until"
,"while"
等(這些都是我知道的基本功能,或至少正在學習,並想利用這些如果可能的一些變化來解決),但每次嘗試我似乎無法弄清楚如何應用停止循環的用戶名(通過最終爲零),回到我的主要目標中,向散列添加新的用戶/密碼。
你需要創建只有一個用戶?還是很多? – 2013-03-14 18:12:32
我需要許多設施,雖然只有一個。 – jmitchell1009 2013-03-14 18:59:48