从ruby中的文件中检索哈希

时间:2014-05-02 11:21:40

标签: ruby hash ruby-1.9.3 ruby-1.9

我有一个场景,我在abc file中存储了一个哈希值。

这是我的abc文件

{
    "access_key"=>"XXXXXXXXXXXXXXXXXXX", 
    "secret_access_key"=>"XXXXXXXXXXXXXXXXXXX",  
    "bucket"=>"XXXXXXXXXXXXXXXXXXX", 
    "name_prefix"=>"my_videos/178/4406/", 
    "x-amz-security-token"=>"AQoDYXdzEBQa4AK5TxvWJM/xsONxl/9ZDVxJc0s9CY+A/yrbhF7/3fK8ZWxlEibuMiEGEzzJ+UcfXBKdOu7oJR2X8l9HqhAD5JmZ2+JJuZjVG9hqP1RPkoQysxXBCeGdOVqOSPk0kW/5sPUG4bjiBbP8WGR9ibRkEq3tGfYazC//UuAZIJDUe+R8FSZay2Izx8BZj3XwPWjF3DsSaWcTIbsRQlMlEmQHD6n7BDv022hNfX13Zf4U18lzft8Sv98etslTC3pbmRd6AbM1I6rK6hn6fJKmrcHYHD3OCAcC2JDWzsv270gBzv1wY4Uma3fU/3HapMIQ5Xb7TU7hlhdHDYjo76FgPRLUPTnw9bXKuWHjG9LVONJuu1aqymlY9iEwASq7Ugk/8w6IMGsRxSeFlbhI689HThukObsQKCpUk2URQwL21fu7/fExUWA5pU5LPwvDgxo0V4Q7JplNwdnXS62Dt3PEj/DmuxfXIM3mjZsF", 
    "expires"=>1999196123
}

现在我必须检索该哈希值。这样我就可以阅读该文件并获得access_keyname_prefix的价值。

我如何在ruby中执行此操作?

3 个答案:

答案 0 :(得分:3)

Ruby Hash.to_s不是双向序列化,因此格式不能轻易反序列化。

如果文件中的数据足够可预测,您可以将文件内容转换为更类似JSON的格式,然后对其进行反序列化:

require 'json'

values = JSON.parse(file_contents.gsub("=>", ": "))

values['access_key']
# => "access_key"=>"XXXXXXXXXXXXXXXXXXX"
values['name_prefix']
# => "my_videos/178/4406/"

如果您可以控制文件的编写方式,我建议您不要使用Hash.to_s,而是使用hash.to_json来编写文件:

puts values.to_json
#=> {
#=>     "access_key": "XXXXXXXXXXXXXXXXXXX", 
#=>     "secret_access_key": "XXXXXXXXXXXXXXXXXXX",  
#=>     "bucket": "XXXXXXXXXXXXXXXXXXX", 
#=>     "name_prefix": "my_videos/178/4406/", 
#=>     "x-amz-security-token": "AQoDYXdzEBQa4AK5TxvWJM/xsONxl/9ZDVxJc0s9CY+A/yrbhF7/3fK8ZWxlEibuMiEGEzzJ+UcfXBKdOu7oJR2X8l9HqhAD5JmZ2+JJuZjVG9hqP1RPkoQysxXBCeGdOVqOSPk0kW/5sPUG4bjiBbP8WGR9ibRkEq3tGfYazC//UuAZIJDUe+R8FSZay2Izx8BZj3XwPWjF3DsSaWcTIbsRQlMlEmQHD6n7BDv022hNfX13Zf4U18lzft8Sv98etslTC3pbmRd6AbM1I6rK6hn6fJKmrcHYHD3OCAcC2JDWzsv270gBzv1wY4Uma3fU/3HapMIQ5Xb7TU7hlhdHDYjo76FgPRLUPTnw9bXKuWHjG9LVONJuu1aqymlY9iEwASq7Ugk/8w6IMGsRxSeFlbhI689HThukObsQKCpUk2URQwL21fu7/fExUWA5pU5LPwvDgxo0V4Q7JplNwdnXS62Dt3PEj/DmuxfXIM3mjZsF", 
#=>     "expires": 1999196123
#=> }

这将允许您跳过上面代码中的gsub,并且比当前情况更加强大。

答案 1 :(得分:3)

使用eval

<强> file.txt的

{
  "access_key"=>"XXXXXXXXXXXXXXXXXXX", 
    "secret_access_key"=>"XXXXXXXXXXXXXXXXXXX",  
    "bucket"=>"XXXXXXXXXXXXXXXXXXX", 
    "name_prefix"=>"my_videos/178/4406/", 
    "x-amz-security-token"=>"AQoDYXdzEBQa4AK5TxvWJM/xsONxl/9ZDVxJc0s9CY+A/yrbhF7/3fK8ZWxlEibuMiEGEzzJ+UcfXBKdOu7oJR2X8l9HqhAD5JmZ2+JJuZjVG9hqP1RPkoQysxXBCeGdOVqOSPk0kW/5sPUG4bjiBbP8WGR9ibRkEq3tGfYazC//UuAZIJDUe+R8FSZay2Izx8BZj3XwPWjF3DsSaWcTIbsRQlMlEmQHD6n7BDv022hNfX13Zf4U18lzft8Sv98etslTC3pbmRd6AbM1I6rK6hn6fJKmrcHYHD3OCAcC2JDWzsv270gBzv1wY4Uma3fU/3HapMIQ5Xb7TU7hlhdHDYjo76FgPRLUPTnw9bXKuWHjG9LVONJuu1aqymlY9iEwASq7Ugk/8w6IMGsRxSeFlbhI689HThukObsQKCpUk2URQwL21fu7/fExUWA5pU5LPwvDgxo0V4Q7JplNwdnXS62Dt3PEj/DmuxfXIM3mjZsF", 
    "expires"=>1999196123
}

代码:

my_hash = eval(File.read('file.txt'))
=> {"access_key"=>"XXXXXXXXXXXXXXXXXXX",
 "secret_access_key"=>"XXXXXXXXXXXXXXXXXXX",
 "bucket"=>"XXXXXXXXXXXXXXXXXXX",
 "name_prefix"=>"my_videos/178/4406/",
 "x-amz-security-token"=>
  "AQoDYXdzEBQa4AK5TxvWJM/xsONxl/9ZDVxJc0s9CY+A/yrbhF7/3fK8ZWxlEibuMiEGEzzJ+UcfXBKdOu7oJR2X8l9HqhAD5JmZ2+JJuZjVG9hqP1RPkoQysxXBCeGdOVqOSPk0kW/5sPUG4bjiBbP8WGR9ibRkEq3tGfYazC//UuAZIJDUe+R8FSZay2Izx8BZj3XwPWjF3DsSaWcTIbsRQlMlEmQHD6n7BDv022hNfX13Zf4U18lzft8Sv98etslTC3pbmRd6AbM1I6rK6hn6fJKmrcHYHD3OCAcC2JDWzsv270gBzv1wY4Uma3fU/3HapMIQ5Xb7TU7hlhdHDYjo76FgPRLUPTnw9bXKuWHjG9LVONJuu1aqymlY9iEwASq7Ugk/8w6IMGsRxSeFlbhI689HThukObsQKCpUk2URQwL21fu7/fExUWA5pU5LPwvDgxo0V4Q7JplNwdnXS62Dt3PEj/DmuxfXIM3mjZsF",
 "expires"=>1999196123}

尽管如此,这只是您病情的一种解决方法。我建议您使用JSONYAML等固定数据格式将数据保存在文件中,然后在代码中重新获取。

答案 2 :(得分:2)

不要像这样存放。将其存储在yaml文件中:

access_key: XXXXXXXXXXXXXXXXXXX 
secret_access_key: XXXXXXXXXXXXXXXXXXX
bucket: XXXXXXXXXXXXXXXXXXX 
name_prefix: my_videos/178/4406/ 
x-amz-security-token: AQoDYXdzEBQa4AK5TxvWJM/xsONxl/9ZDVxJc0s9CY+A/yrbhF73fK8ZWxlEibuMiEGEzzJ+UcfXBKdOu7oJR2X8l9HqhAD5JmZ2+JJuZjVG9hqP1RPkoQysxXBCeGdOVqOSPk0kW/5sPUG4bjiBbP8WGR9ibRkEq3tGfYazC/UuAZIJDUe+R8FSZay2Izx8BZj3XwPWjF3DsSaWcTIbsRQlMlEmQHD6n7BDv022hNfX13Zf4U18lzft8Sv98etslTC3pbmRd6AbM1I6rK6hn6fJKmrcHYHD3OCAcC2JDWzsv270gBzv1wY4Uma3f/3HapMIQ5Xb7TU7hlhdHDYjo76FgPRLUPTnw9bXKuWHjG9LVONJuu1aqymlY9iEwASq7Ugk/8w6IMGsRxSeFlbhI689HThukObsQKCpUk2URQwL21fu7/fExUWA5pU5LPwvDgxo0V4Q7JplNwdnXS62Dt3PEjDmuxfXIM3mjZsF 
expires: 1999196123

然后只需加载:

my_hash = YAML.load File.open('/path/to/yaml/file')

my_hash['access_key']     #=> 'XXXXXXXXXXXXXXXX'