find_index(...)
public
Compares each entry in enum with value or passes to
block. Returns the index for the first for which the evaluated value is
non-false. If no object matches, returns nil
(1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 }
(1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 }
(1..100).find_index(50)
Show source
/*
* call-seq:
* enum.find_index(value) => int or nil
* enum.find_index {| obj | block } => int or nil
*
* Compares each entry in <i>enum</i> with <em>value</em> or passes
* to <em>block</em>. Returns the index for the first for which the
* evaluated value is non-false. If no object matches, returns
* <code>nil</code>
*
* (1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> 34
* (1..100).find_index(50) #=> 49
*
*/
static VALUE
enum_find_index(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE memo[3]; /* [return value, current index, condition value] */
rb_block_call_func *func;
if (argc == 0) {
RETURN_ENUMERATOR(obj, 0, 0);
func = find_index_iter_i;
}
else {
rb_scan_args(argc, argv, "1", &memo[2]);
if (rb_block_given_p()) {
rb_warn("given block not used");
}
func = find_index_i;
}
memo[0] = Qnil;
memo[1] = 0;
rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
return memo[0];
}