可能重复:
Is there a clean way to avoid calling a method on nil in a nested params hash?测试红宝石多维散列
是否有一个简单的方法来检查,如果在一多维散列值,而没有设置醒目的NoMethodException?
例:
do_x if cat['level1']['level2']['level3'] != 'value'
的问题是,“(第二级)”可能根本不存在,因此接收为零。
在PHP中,你可以在前面加一个'@'来抑制错误。
在Ruby中,我将不得不将检查包裹在开始救援块中。
是否有像PHP一样的快速解决方案?
多重复:http://stackoverflow.com/q/5429790/188031 – tokland