Fetch vs. [] for hash keys
foo = { :bar =>"x", :baz => "y" }
foo[:bar] = "x"
foo.fetch(:bar, :default) = "x"
fetch
will raise an exception (KeyError) if the key doesn't exist and a default isn't provided.
Written by Melanie Archer
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#