class Array

Public Instance Methods

index( obj) → int or nil click to toggle source
index() { |elem| ... } → int or nil

Returns the index of the first object in self such that is == to obj or the block returns true. If no match is found, nil is returned.

a = %w(a b c d e)
a.index("b")               #=> 1
a.index("z")               #=> nil
a.index { |e| e >= "b" }   #=> 1
a.index { |e| e >= "q" }   #=> nil
VALUE
rb_ary_index( int argc, VALUE *argv, VALUE ary)
{
    VALUE val;

    if (rb_scan_args( argc, argv, "01", &val) == 1) {
        if (rb_block_given_p())
            rb_warning( "given block not used");
        return supplement_index_val( ary, val);
    } else
        return supplement_index_blk( ary);
    return Qnil;
}
indexes() → ary click to toggle source
keys() → ary

Returns the indexes from 0 to array.length() as an array.

[ "a", "h", "q"].indexes   #=> [ 0, 1, 2]
VALUE
rb_ary_indexes( VALUE ary)
{
    VALUE ret;
    int i, j;

    j = RARRAY_LEN( ary);
    ret = rb_ary_new2( j);
    for (i = 0; j; ++i, --j) {
        rb_ary_push( ret, INT2FIX( i));
    }
    return ret;
}
Also aliased as: keys
keys()
Alias for: indexes
notempty? → nil or self click to toggle source

Returns self if and only if ary is not empty, nil otherwise.

%w(a b).notempty?   #=> [ "a", "b"]
[].notempty?        #=> nil
VALUE
rb_ary_notempty_p( VALUE ary)
{
    return RARRAY_LEN( ary) == 0 ? Qnil : ary;
}
pick( ref) → obj or nil click to toggle source
pick { |elem| ... } → obj or nil

Deletes the element where first ref === obj is true or the block first returns true. The result will be nil if nothing is found.

a = %w(ant bat cat dog)
a.pick { |e| e =~ /^c/ }  #=> "cat"
a                         #=> ["ant", "bat", "dog"]
a.pick { |e| e =~ /^x/ }  #=> nil
VALUE
rb_ary_pick( int argc, VALUE *argv, VALUE ary)
{
    VALUE ref;
    VALUE pos;
    VALUE p;

    if (rb_scan_args( argc, argv, "01", &ref) == 1)
        pos = supplement_index_ref( ary, ref);
    else
        pos = supplement_index_blk( ary);

    if (!NIL_P( pos))
        return rb_funcall( ary, id_delete_at, 1, pos);
    return Qnil;
}
rindex( obj) → int or nil click to toggle source
rindex() { |elem| ... } → int or nil

Returns the index of the first object in self such that is == to obj or the block returns true. If no match is found, nil is returned. Search from right to left.

a = %w(a b c d e)
a.rindex("b")               #=> 1
a.rindex("z")               #=> nil
a.rindex { |e| e >= "b" }   #=> 4
a.rindex { |e| e >= "q" }   #=> nil
VALUE
rb_ary_rindex( int argc, VALUE *argv, VALUE ary)
{
    VALUE val;

    if (rb_scan_args( argc, argv, "01", &val) == 1) {
        if (rb_block_given_p())
            rb_warning( "given block not used");
        return supplement_rindex_val( ary, val);
    } else
        return supplement_rindex_blk( ary);
    return Qnil;
}
rpick( ref) → obj or nil click to toggle source
rpick { |elem| ... } → obj or nil

Deletes the element where first ref === obj is true or the block first returns true. The result will be nil if nothing is found. Search from right to left.

a = %w(ant cow bat cat dog)
a.rpick { |e| e =~ /^c/ }  #=> "cat"
a                          #=> ["ant", "cow", "bat", "dog"]
a.rpick { |e| e =~ /^x/ }  #=> nil
VALUE
rb_ary_rpick( int argc, VALUE *argv, VALUE ary)
{
    VALUE ref;
    VALUE pos;
    VALUE p;

    if (rb_scan_args( argc, argv, "01", &ref) == 1)
        pos = supplement_rindex_ref( ary, ref);
    else
        pos = supplement_rindex_blk( ary);

    if (!NIL_P( pos))
        return rb_funcall( ary, id_delete_at, 1, pos);
    return Qnil;
}
select! { |x| ... } → ary click to toggle source

Remove all items for that the block returns nil or false.

VALUE
rb_ary_select_bang( VALUE self)
{
    return rb_iterate( &supplement_reject, self,
                                &supplement_invert_yield, Qnil);
}