Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns a new Array whose elements are the elements of self at the given Integer or Range indexes.
For each positive index, returns the element at offset index:
a = [:foo, 'bar', 2]
a.values_at(0, 2) # => [:foo, 2]
a.values_at(0..1) # => [:foo, "bar"]
The given indexes may be in any order, and may repeat:
a = [:foo, 'bar', 2]
a.values_at(2, 0, 1, 0, 2) # => [2, :foo, "bar", :foo, 2]
a.values_at(1, 0..2) # => ["bar", :foo, "bar", 2]
Assigns nil for an index that is too large:
a = [:foo, 'bar', 2]
a.values_at(0, 3, 1, 3) # => [:foo, nil, "bar", nil]
Returns a new empty Array if no arguments given.
For each negative index, counts backward from the end of the array:
a = [:foo, 'bar', 2]
a.values_at(-1, -3) # => [2, :foo]
Assigns nil for an index that is too small:
a = [:foo, 'bar', 2]
a.values_at(0, -5, 1, -6, 2) # => [:foo, nil, "bar", nil, 2]
The given indexes may have a mixture of signs:
a = [:foo, 'bar', 2]
a.values_at(0, -2, 1, -1) # => [:foo, "bar", "bar", 2]
 
               static VALUE
rb_ary_values_at(int argc, VALUE *argv, VALUE ary)
{
    long i, olen = RARRAY_LEN(ary);
    VALUE result = rb_ary_new_capa(argc);
    for (i = 0; i < argc; ++i) {
        append_values_at_single(result, ary, olen, argv[i]);
    }
    RB_GC_GUARD(ary);
    return result;
}
            

Was this page useful?