我想做的是传入一个看起来像这样的哈希值:
input = {
"configVersion" => "someVers",
"box" =>
{
"primary" => {
"ip" => "192.168.1.1",
"host" => "something"
},
"api" => {
"live" => "livekey",
"test" => "testkey"
}
}
}
然后迭代它,如果该值是另一个散列,则继续,并用它生成输出。结果应该是这样的:
configVersion = "someVers"
box.primary.ip = "192.168.1.1"
box.primary.host = "something"
等等...
如果值是散列,我知道如何爬行并继续,但我不确定如何将整个事物连接在一起并将值传递回去。这是我的代码:
def crawl(input)
input.each do |k,v|
case v
when Hash
out < "#{k}."
crawl(v)
else
out < " = '#{v}';"
end
end
end
我的问题是:在哪里定义out
以及如何将其全部返回。我对Ruby很陌生。