fetch vs. [] when working with hashes?
This question already has an answer here:
From Ruby Koans about_hashes.rb
:
Why might you want to use #fetch
instead of #[]
when accessing hash keys?
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 aKeyError
if it doesn'tfetch(key_name, default_value)
: get the value if the key exists, returndefault_value
otherwisefetch(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.
With []
, the creator of the hash controls what happens when a key does not exist, with fetch
you do.
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
참고URL : https://stackoverflow.com/questions/16569409/fetch-vs-when-working-with-hashes
'Nice programing' 카테고리의 다른 글
GIS : PostGIS / PostgreSQL vs. MySql vs. SQL Server? (0) | 2020.11.17 |
---|---|
jasper를 얻기 위해 jrxml을 어떻게 컴파일합니까? (0) | 2020.11.17 |
Why does Java let you cast to a collection? (0) | 2020.11.16 |
C# Convert a Base64 -> byte[] (0) | 2020.11.16 |
How to find index position of an element in a list when contains returns true (0) | 2020.11.16 |