从ruby中的文件中检索散列

时间:2021-02-02 15:58:35

I have scenario as, I have stored a hash value in the abc file.

假设,我在abc文件中存储了一个散列值。

Here is my 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
}

now I have to retrieve that hash. So that I can read the file and get value of access_key, name_prefix.

现在我要检索那个散列。这样我就可以读取文件并获取access_key, name_prefix的值。

How can I do this in ruby?

如何在ruby中实现这一点?

3 个解决方案

#1


2  

Do not store it like this. Store it in a yaml file:

不要这样储存。将它存储在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

And then just load it with:

然后装上:

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

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

#2


3  

Use eval

使用eval

file.txt

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
}

Code:

代码:

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}

Though, this is just a workaround for your condition. I will recommend you to use fixed data formats like JSON or YAML to persist data in files and then fetch it back in your code.

不过,这只是您的工作环境。我建议您使用固定的数据格式,如JSON或YAML,将数据保存在文件中,然后在代码中取回。

#3


3  

Ruby's Hash.to_s is not a two-way serialization, so the format cannot be readily de-serialized.

Ruby的散列。to_s不是双向序列化,因此不能很容易地反序列化格式。

If the data in file is predictable enough, you can convert the file contents to a more JSON-like format, and then deserialize it:

如果文件中的数据足够可预测,您可以将文件内容转换为更像json的格式,然后反序列化:

require 'json'

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

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

If you have control on how your file is written, I will suggest that you do not use Hash.to_s, but rather hash.to_json to write your file:

如果您能够控制如何编写文件,我建议您不要使用散列。to_s,而是散列。写入文件的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
#=> }

This will allow you to skip the gsub in the code above, and is a lot more robust than the current situation.

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

#1


2  

Do not store it like this. Store it in a yaml file:

不要这样储存。将它存储在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

And then just load it with:

然后装上:

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

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

#2


3  

Use eval

使用eval

file.txt

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
}

Code:

代码:

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}

Though, this is just a workaround for your condition. I will recommend you to use fixed data formats like JSON or YAML to persist data in files and then fetch it back in your code.

不过,这只是您的工作环境。我建议您使用固定的数据格式,如JSON或YAML,将数据保存在文件中,然后在代码中取回。

#3


3  

Ruby's Hash.to_s is not a two-way serialization, so the format cannot be readily de-serialized.

Ruby的散列。to_s不是双向序列化,因此不能很容易地反序列化格式。

If the data in file is predictable enough, you can convert the file contents to a more JSON-like format, and then deserialize it:

如果文件中的数据足够可预测,您可以将文件内容转换为更像json的格式,然后反序列化:

require 'json'

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

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

If you have control on how your file is written, I will suggest that you do not use Hash.to_s, but rather hash.to_json to write your file:

如果您能够控制如何编写文件,我建议您不要使用散列。to_s,而是散列。写入文件的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
#=> }

This will allow you to skip the gsub in the code above, and is a lot more robust than the current situation.

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