class Array
Public Instance Methods
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; }
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; }
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; }
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; }
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; }
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; }
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); }