log*_*han 3 ruby ruby-on-rails
我有一个从 ruby 脚本生成的文件,它使用 hashie gem 将哈希转换为 mash,如下所示
config.ip_details.ip_one.pub.heartbeat.ip1 |"198.18.0.2"|
config.vpc.vms.vse_pub.heartbeat.netmask |"255.255.255.252"|
Run Code Online (Sandbox Code Playgroud)
而且我想读取文件并需要将 Mash 字符串转换为 Hash,它们是否可以在 hashie gem 中使用任何方法将 mash 字符串转换为哈希?
有一种to_hash
方法可以调用 Mash 对象来获取哈希值。
require "hashie"
hash = {:b => 23, :d => {:e => "abc"}}
mash = Hashie::Mash.new(hash)
p mash
#=> #<Hashie::Mash b=23 d=#<Hashie::Mash e="abc">>
p mash.to_hash
#=> {"b"=>23, "d"=>{"e"=>"abc"}}
Run Code Online (Sandbox Code Playgroud)
要从Mash
文件 (YAML)创建对象,您可以使用Mash#load
方法:
mash = Hashie::Mash.load("sample.yml")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2249 次 |
最近记录: |