4 ms·
I don't know ruby and if it's the same, but if you're not using a library like funcy or toolz for a nested get helper, you can do `dict.get('key', {}).get('key2
by code_biologist 3y ago
I don't know ruby and if it's the same, but if you're not using a library like funcy or toolz for a nested get helper, you can do `dict.get('key', {}).get('key2', {}).get('key3')`. Not the prettiest, but can do in a pinch.
- irjustin 3y agoThanks! i didn't know about these and will check them out for sure. Tired of `if key in hash:` nested layers.
- nicolaslem 3y agoIn my opinion, a better alternative to nested `if key in hash`: try: value = data["foo"]["bar"]["baz"] except KeyError: value = None
- irjustin 3y agoI know it's been almost a year but I still haven't accepted this pattern. Using errors as general flow control makes me uncomfortable. It shouldn't be an error or exception except in.... Actual problems.