Cons to using
- 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.
fetchif 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 that
ais always a hash. Strings also have the method
but doesn’t have the method
fetchso you can find errors easier.
fetch: Easier to debug
KeyNotFoundrather than letting a nil string trickle into your code and have to debug whatever that happens to cause.
- You can also use