ruby 以hash方式读取.rb文件

WO浣熊OW 2015-04-07 03:47:07
Chef中的attribute file都是XXX.rb格式,有什么方法可以把这种XXX.rb格式的文件转换成.json格式的文件吗?
或者
像JSON.parse()方法一样,把.rb文件读取成一种obj类型的hash。

如:

$test['apache']['httpd.conf'] = {
'ServerTokens' => "ProductOnly",
'deflate' => {
'DeflateCompressionLevel' => '1'
},
'KeepAlive' => "On",
'KeepAliveTimeout' => "5",
'ListenBackLog' => "511"
}

其中$test['apache']['httpd.conf']是key,
而{
'ServerTokens' => "ProductOnly",
'deflate' => {
'DeflateCompressionLevel' => '1'
},
'KeepAlive' => "On",
'KeepAliveTimeout' => "5",
'ListenBackLog' => "511"
}是value,依次递归这样读取。

请问有实现的方法吗?
...全文
217 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
WO浣熊OW 2015-04-09
  • 打赏
  • 举报
回复
$test['apache']['httpd.conf'] = {'ServerTokens' => "ProductOnly",'deflate' => {'DeflateCompressionLevel' => '1'},'KeepAlive' => "On",'KeepAliveTimeout' => "5",'ListenBackLog' => "511"} 请问以上这样的字符串,用ruby怎么将它转成hash类型的?
WO浣熊OW 2015-04-08
  • 打赏
  • 举报
回复
怎么把大{}的value付给一个变量?

3,424

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧