帮助,我是一个菜鸟,只需要对这段代码提出一些建议。我的大部分程序都在工作,这部分让我感到困惑,我想得到一个名字和密码。然后将名称作为键,将密码作为值。现在它必须是用户定义的..然后我必须能够再次拉动该哈希信息。我认为返回会起作用......这是我的代码
def login_prompt
vault = {}
puts "WELCOME! please enter an existing username: "
username = gets.chomp
checkname = Noxread.new
comparename = checkname.read_file
comparename.keys.include?("#{username}")
if comparename == true
puts "please enter your password: "
password = gets.chomp
vault[username]= password
else puts "username already exists!! would you like to retry? (y/n)"
case answer
when /^y/
login_prompt
when /^n/
exit
end
end
end
所以应该收集信息。这是我的代码来合并它和我从文件中提取的哈希。在NoxRead类中
require_relative' read' 要求' csv'
class Noxwrite
attr_accessor :name :password
def initialize
@name = name
@password = password
end
def upsum
x = Noxread.new
y = x.read_file
z = login_prompt
y.merge(z) {|name, password| name + ',' + password}
return y
end
def write_file
ehash = upsum
CSV.open("data.csv", "wb") do |csv|
csv << ehash
end
end
end
答案 0 :(得分:0)
此代码有什么问题。看起来很好,除了不能在开放文本中这样读取密码。
当你写
之类的东西时 user_hash = login_prompt
user_hash将根据需要使用哈希
{"username"=>"password"}