0

帮助,我是菜鸟,只需要对这段代码的一些建议。我的大部分程序都在工作,这部分让我惊呆了,我想得到一个名字和密码。然后将名称设为密钥,将密码设为值。现在它必须是用户定义的。然后我必须能够再次提取该哈希信息。我认为返回会起作用......这是我的代码

  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
4

1 回答 1

0

这段代码有什么问题。看起来很好,除了密码不应该像这样在开放文本中读取。

当你写类似的东西时

user_hash = login_prompt

user_hash 将具有所需的哈希值

{"username"=>"password"}

于 2011-11-18T10:31:39.737 回答