Redis客户端使用Ruby

时间:2015-11-17 03:01:08

标签: ruby redis

我正在尝试获取 AGE 属性的用户输入,并将其与我的键值对进行比较,并尝试返回与用户输入值匹配的记录。

代码:

require "redis"
require "csv"
require "json"

begin
  redis = Redis.new(:url => "redis://h:petr08jsjnofis211gmbq8fdd8@ec2-54-83-9-36.compute-1.amazonaws.com:11959")
  puts "Connected"

  health = CSV.read('6339_Dataset_1.csv')

  health.each do |data_row|
    redis.hmset(data_row[21],"AGE",data_row[0],"SEX",data_row[1],"RACE",data_row[2],"DAY_OF_ADMISSION",data_row[3],"DISCHARGE_STATUS",data_row[4],"STAY_INDICATOR",data_row[5],"ID",data_row[21])
  end

  value = redis.keys('*')
  puts "QUERY 2"
  puts "Enter the AGE:"
  age = gets

  value.each do |data|
    #puts id
    val = redis.hgetall(data)
    val.each do |key, value|
        if key == "AGE" && value == age
            puts redis.hgetall(data)
        else
            puts "NO"
        end
    end     
  end
end

问题:  如果我硬编码 AGE ,我可以得到输出但是如果我得到用户输入,它就会转到其他部分。

1 个答案:

答案 0 :(得分:0)

尝试在您检查 AGE 键的行中使用.to_i,例如:

if key == "AGE" && value.to_i == age.to_i

此外,第二个value变量会影响外部value变量。 最好将value = redis.keys('*')更改为类似的内容,

 keys = redis.keys('*')