我遇到像这样的哈希
{"num"=>"219", "id"=>"219", "name"=>"219", "key"=>"", "ps"=>["ˈɑ:bitrəri", "ˈɑrbɪˌtrɛri"], "sent"=>[{"orig"=>"\nHe makes unpredictable, decisions.\n", "trans"=>"\his decision is very hard to understand \n"}, {"orig"=>"\nYou can make an choice.\n", "trans"=>"\n you can chose randomly。\n"}]}
我只想打印此哈希的一部分。
我的解决方案是
key = ['key','ps','sent']
key.each{|key| key == 'sent' ? (p server_config["sent"].to_s) : (p server_config[key])}
效果不佳。 像这样的两级哈希打印
[{\"orig\"=>\"\\nAs soon as he kicked the bucket, he started to become famous.\\n\", \"trans\"=>\"\\nhe die and he became famous \\n\"}, ]"
如何打印这两级哈希
我想要的输出如下所示。
As soon as he kicked the bucket, he started to become famous.
he die and he became famous.
答案 0 :(得分:0)
如果输出格式不是constrait,您可以尝试pretty print库:
require 'pp'
[ 'key','ps','sent' ].each do |key|
PP.pp(data[key])
end
答案 1 :(得分:0)
您已嵌入阵列和哈希。我告诉你一种接近它的方法。应该让你开始。
my_hash = {"num"=>"219", "id"=>"219", "name"=>"219", "key"=>"", "ps"=>["a:bitreri", "arbitreri"], "sent"=>[{"orig"=>"\nHe makes unpredictable, decisions.\n", "trans"=>"\his decision is very hard to understand \n"}, {"orig"=>"\nYou can make an choice.\n", "trans"=>"\n you can chose randomly.\n"}]}
my_hash["sent"].each{|item| item.each {|key, val| puts val}}
祝你好运!