Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
ext/backport_dig/backport_dig.c

Instance Method Summary collapse

Instance Method Details

#dig(key, ...) ⇒ Object

Extracts the nested value specified by the sequence of idx objects by calling dig at each step, returning nil if any intermediate step is nil.

h = { foo: {bar: {baz: 1}}}

h.dig(:foo, :bar, :baz)           #=> 1
h.dig(:foo, :zot, :xyz)           #=> nil

g = { foo: [10, 11, 12] }
g.dig(:foo, 1)                    #=> 11

Returns:

  • (Object)


89
90
91
92
93
94
95
96
97
# File 'ext/backport_dig/backport_dig.c', line 89

VALUE
rb_hash_dig(int argc, VALUE *argv, VALUE self)
{
    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    self = rb_hash_aref(self, *argv);
    if (!--argc) return self;
    ++argv;
    return rb_obj_dig(argc, argv, self, Qnil);
}