How can I store user defined data in a hash

2019-06-13 16:12发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-06-13 16:53

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"}

查看更多
登录 后发表回答