APIdock / Ruby
/
method

select

v2_5_5 - Show latest stable - Class: Array
select()
public

Returns a new array containing all elements of ary for which the given block returns a true value.

If no block is given, an Enumerator is returned instead.

[1,2,3,4,5].select  { |num| num.even? } #=> [2, 4]
a = %w{ a b c d e f }
a.select  { |v| v =~ /[aeiou]/ } #=> ["a", "e"]

See also Enumerable#select.

static VALUE
rb_ary_select(VALUE ary)
{
 VALUE result;
 long i;
 RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
 result = rb_ary_new2(RARRAY_LEN(ary));
 for (i = 0; i < RARRAY_LEN(ary); i++) {
 if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) {
 rb_ary_push(result, rb_ary_elt(ary, i));
 }
 }
 return result;
}

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