By default, using #[]
will retrieve the hash value if it exists, and return nil if it doesn't exist *.
Using #fetch
gives you a few options (see the docs on #fetch):
fetch(key_name)
: get the value if the key exists, raise a KeyError
if it doesn't
fetch(key_name, default_value)
: get the value if the key exists, return default_value
otherwise
fetch(key_name) { |key| "default" }
: get the value if the key exists, otherwise run the supplied block and return the value.
Each one should be used as the situation requires, but #fetch
is very feature-rich and can handle many cases depending on how it's used. For that reason I tend to prefer it over accessing keys with #[]
.
* As Marc-André Lafortune said, accessing a key with #[]
will call #default_proc
if it exists, or else return #default
, which defaults to nil
. See the doc entry for ::new for more information.