APIdock / Ruby
/
method

dig

ruby latest stable - Class: Struct
dig(*args)
public

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

Foo  = Struct .new (:a)
f = Foo .new (Foo .new ({b: [1, 2, 3]}))
f.dig (:a, :a, :b, 0) # => 1
f.dig (:b, 0) # => nil
f.dig (:a, :a, :b, :c) # TypeError: no implicit conversion of Symbol into Integer
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);
}

AltStyle によって変換されたページ (->オリジナル) /