grep(p1)
public
Returns an array of every element in enum for which Pattern
=== element. If the optional block is supplied, each matching
element is passed to it, and the block’s result is stored in the
output array.
(1..100).grep 38..44
c = IO.constants
c.grep(/SEEK/)
res = c.grep(/SEEK/) {|v| IO.const_get(v) }
res
Show source
/*
* call-seq:
* enum.grep(pattern) => array
* enum.grep(pattern) {| obj | block } => array
*
* Returns an array of every element in <i>enum</i> for which
* <code>Pattern === element</code>. If the optional <em>block</em> is
* supplied, each matching element is passed to it, and the block's
* result is stored in the output array.
*
* (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
* c = IO.constants
* c.grep(/SEEK/) #=> ["SEEK_END", "SEEK_SET", "SEEK_CUR"]
* res = c.grep(/SEEK/) {|v| IO.const_get(v) }
* res #=> [2, 0, 1]
*
*/
static VALUE
enum_grep(obj, pat)
VALUE obj, pat;
{
VALUE ary = rb_ary_new();
VALUE arg[2];
arg[0] = pat;
arg[1] = ary;
rb_iterate(rb_each, obj, rb_block_given_p() ? grep_iter_i : grep_i, (VALUE)arg);
return ary;
}