Method: Array#select
- Defined in:
- array.c
#select {|item| ... } ⇒ Array #select ⇒ Enumerator #filter {|item| ... } ⇒ Array #filter ⇒ Enumerator
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.
Array#filter is an alias for Array#select.
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 |
# File 'array.c', line 3212 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; } |