Here’s Hash#dig!
, a handy little piece of code I wrote for a project. It’s similar to Ruby 2.3’s Hash#dig
, but raises an exception instead of returning nil
when a key isn’t found.
places = { world: { uk: true } } places.dig :world, :uk # true places.dig! :world, :uk # true places.dig :world, :uk, :bvi # nil places.dig! :world, :uk, :bvi # KeyError: # Key not found: :bvi
#dig is to #[] as #dig! is to #fetch
Ruby 2.3 introduces the new Hash#dig
method for safe extraction of a nested value. It’s the equivalent of a safely repeated Hash#[]
.
#dig!
, on the other hand, is the equivalent of a safely repeated Hash#fetch
. It’s “safely unsafe” 😉 raising the appropriate KeyError
anywhere down the line, or returning the value if it exists. This turns out to be very easy to implement in the functional style:
def dig!(*keys) keys.reduce(self) { |a, e| a.fetch(e) } end