我如何将用户定义的数据存储在散列中 [英] How can I store user defined data in a hash

查看:114
本文介绍了我如何将用户定义的数据存储在散列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助,我是一个noob,只需要一些关于这一点代码的建议。我已经掌握了大部分我的程序工作,这部分我有了我想要的名字和密码。然后将名称作为键和密码的值。现在它必须是用户定义的。然后我必须能够再次提取哈希信息。我认为,返回将工作...这是我的代码

  def login_prompt 
vault = {}
输入WELCOME!请输入一个现有的用户名:
username = gets.chomp
checkname = Noxread.new
comparename = checkname.read_file
comparename.keys.include?( #{username})
if comparename == true
puts请输入您的密码:
password = gets.chomp
保险柜[用户名] =密码$ b $ (y / n)
case answer
when / ^ y /
login_prompt
when / ^ n /
出口
结束
结束
结束

so应该收集信息。这是我的合并代码和我从文件中提取的哈希。在NoxRead类中



require_relative'read'
require'csv'

  class Noxwrite 
attr_accessor:name:password

def初始化
@name = name
@password =密码
结束

def upsum

x = Noxread.new
y = x.read_file
z = login_prompt
y.merge(z){| name,password | name +','+ password}
返回y

结束

def写入文件

ehash = upsum
CSV。打开(data.csv,wb)do | csv |
csv<< ehash
end

end

end


解决方案

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



当您写入类似于

user_hash = login_prompt



user_hash将根据需要设置散列值

{username=>password}


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

解决方案

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

这篇关于我如何将用户定义的数据存储在散列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆