123456789_123456789_123456789_123456789_123456789_

Dig Methods

Ruby's dig methods are useful for accessing nested data structures.

Consider this data: item = { id: "0001", type: "donut", name: "Cake", ppu: 0.55, batters: { batter: [ "1001", type: "Regular", "1002", type: "Chocolate", "1003", type: "Blueberry", "1004", type: "Devil's Food" ] }, topping: [ "5001", type: "None", "5002", type: "Glazed", "5005", type: "Sugar", "5007", type: "Powdered Sugar", "5006", type: "Chocolate with Sprinkles", "5003", type: "Chocolate", "5004", type: "Maple" ] }

Without a dig method, you can write: item[:batter][1][:type] # => "Chocolate"

With a dig method, you can write: item.dig(:batters, :batter, 1, :type) # => "Chocolate"

Without a dig method, you can write, erroneously (raises NoMethodError (undefined method `[]' for nil:NilClass)): item[:BATTER][1][:type]

With a dig method, you can write (still erroneously, but avoiding the exception): item.dig(:batters, :BATTER, 1, :type) # => nil

Why Is dig Better?

How Does dig Work?

The call sequence is:

obj.dig(*identifiers)

The identifiers define a "path" into the nested data structures:

A dig method raises an exception if any receiver does not respond to #dig:

h = { foo: 1 }
# Raises TypeError (Integer does not have #dig method):
h.dig(:foo, :bar)

What Else?

The structure above has Hash objects and Array objects, both of which have instance method dig.

Altogether there are six built-in Ruby classes that have method dig, three in the core classes and three in the standard library.

In the core:

In the standard library: