让我们说我从API取回一个JSON嵌套散列(或散列数组)用nils遍历ruby嵌套散列?
@example = {"results" = > {{"poop" => "shoop"},{"foo" => {"shizz" => "fizz", "nizzle"=>"bizzle"}}}
YAML标记中嵌套的哈希上述
- poop: shoop
- foo:
shizz: fizz
nizzle: bizzle
现在让我们去作数据库条目与ActiveRecord哈希。这工作正常。
Thing.create!(:poop => @example["results"]["poop"],
:shizz => @example["results"]["foo"]["shizz"],
:nizzle=> @example["results"]["foo"]["nizzle"])
但是如果'foo'为空或无?例如,如果一个API结果有一个带有“名字”,“姓氏”#等的“person”散列,那么如果没有数据,那么“person”散列通常是空的,这意味着它内部的散列不会“不存在。
@example = {"results" = > {{"poop" => "shoop"},{"foo" => nil }}
Thing.create!(:poop => @example["results"]["poop"],
:shizz => @example["results"]["foo"]["shizz"],
:nizzle=> @example["results"]["foo"]["nizzle"])
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
处理这个问题的最好方法是什么?
确实很有趣,如何“耗尽”备忘录散列 – maprihoda
光滑 - 这将派上用场 – klochner
所以我会制作一个自定义的类来扩展系统的哈希类,或者我会将这个'get'方法的猴子修补成“哈希”? – thoughtpunch