How could variable be assigned in the easy and straightforward way in this case:
interface = reply[:user][:interface][:data][0][:value].presence || 'simple'
If the array key reply[:user][:interface] exists – I have the whole data chain (in this case) but if it doesn’t – I get the undefined method '[]' for nil:NilClass. In general I want a method to assign default if any of the keys in a path doesn’t exists. Now I do it like:
a = b[:asd][:qwe] rescue 5
which works but doesn’t look like a proper way.
The issue with calling
rescuelike that is that it traps more errors than just the one you want, and so can hide problems. So the “proper way” is to rescue on only the error you expect: