Method: Struct#dig
- Defined in:
- struct.c
permalink #dig(name, *identifiers) ⇒ Object #dig(n, *identifiers) ⇒ Object
Finds and returns an object among nested objects.
The nested objects may be instances of various classes.
See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
Given symbol or string argument +name+,
returns the object that is specified by +name+ and +identifiers+:
Foo = Struct.new(:a)
f = Foo.new(Foo.new({b: [1, 2, 3]}))
f.dig(:a) # => #<struct Foo a={:b=>[1, 2, 3]}>
f.dig(:a, :a) # => {:b=>[1, 2, 3]}
f.dig(:a, :a, :b) # => [1, 2, 3]
f.dig(:a, :a, :b, 0) # => 1
f.dig(:b, 0) # => nil
Given integer argument +n+,
returns the object that is specified by +n+ and +identifiers+:
f.dig(0) # => #<struct Foo a={:b=>[1, 2, 3]}>
f.dig(0, 0) # => {:b=>[1, 2, 3]}
f.dig(0, 0, :b) # => [1, 2, 3]
f.dig(0, 0, :b, 0) # => 1
f.dig(:b, 0) # => nil
1542 1543 1544 1545 1546 1547 1548 1549 1550 |
# File 'struct.c', line 1542
static VALUE
rb_struct_dig(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
self = rb_struct_lookup(self, *argv);
if (!--argc) return self;
++argv;
return rb_obj_dig(argc, argv, self, Qnil);
}
|