TIL, 2017-12-01
Cons to using fetch(:thingie)
vs [:thingie]
?
- If you use
fetch
, use the block form, not the default value form, because the block runs only when the key is missing, whereas the default value runs every time. fetch
if you want to produce an error if no key, or you want to specify a default value.- The problem with
a['key']
is that you are assuming thata
is always a hash. Strings also have the method[]
but doesn’t have the methodfetch
so you can find errors easier. fetch
: Easier to debugKeyNotFound
rather than letting a nil string trickle into your code and have to debug whatever that happens to cause.- You can also use
Hash#dig
andArray#dig
over fetch.