How can I store user defined data in a hash

巧了我就是萌 提交于 2019-12-08 11:48:06

问题


Help, I am a noob, just need some advice on this bit of code. I have got most of my program working this part has me stuped i want to get a name and password. Then make the name the key and the password the value. Now it must be user defined.. Then I must be able to pull that hash info again. I thought that return would work... here is my code

  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

so that should gather the info. and this is my code to merge that and an hash that i pulled from a file. in a NoxRead class

require_relative 'read' require '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

回答1:


What is the problem with this code. Seems fine, apart from the fact that passwords should not be read like this in open text.

When you write something like

user_hash = login_prompt

user_hash will have the hash as desired

{"username"=>"password"}



来源:https://stackoverflow.com/questions/8180776/how-can-i-store-user-defined-data-in-a-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!