Sometimes while dealing with API responses, I'll end up writing something like:
what_i_need = response["key"]["another key"]["another key 2"]
The problem with that is, it'll throw an error if, say, "another key"
is missing. I don't like that. I'd be a lot happier if what_i_need
turned up a nil
if something along the process broke.
Is there a more elegant solution than:
what_i_need = nil
begin
what_i_need = response["key"]["another key"]["another key 2"]
rescue Exception => e
end
I also thought about monkey patching NilClass you try to access nil["something"]
it would return nil
, but I'm not sure if that's the best way to go about it either of if it's possible even.