Last Updated: February 25, 2016
·
351
· mejarc

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.