Home » Ruby » fetch vs. [] when working with hashes? [duplicate]

fetch vs. [] when working with hashes? [duplicate]

Posted by: admin November 30, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

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.

Questions:
Answers:

With [], the creator of the hash controls what happens when a key does not exist, with fetch you do.

Questions:
Answers:

fetch by default raises an error if the key is not found. You can supply a default value instead.

h = {}

h.fetch(:foo) # no default value, raises error
# => # ~> -:3:in `fetch': key not found: :foo (KeyError)

h.fetch(:bar, 10) # default value, returns default value
# => 10