/*
* call-seq:
* enum.count(item) => int
* enum.count {| obj | block } => int
*
* Returns the number of items in <i>enum</i> for which equals to <i>item</i>.
* If a block is given, counts the number of elements yielding a true value.
*
* ary = [1, 2, 4, 2]
* ary.count(2) # => 2
* ary.count{|x|x%2==0} # => 3
*
*/
static VALUE
enum_count(int argc, VALUE *argv, VALUE obj)
{
if (argc == 1) {
VALUE item, args[2];
if (rb_block_given_p()) {
rb_warn("given block not used");
}
rb_scan_args(argc, argv, "1", &item);
args[0] = item;
args[1] = 0;
rb_block_call(obj, id_each, 0, 0, count_i, (VALUE)&args);
return INT2NUM(args[1]);
}
else if (argc == 0) {
long n;
RETURN_ENUMERATOR(obj, 0, 0);
n = 0;
rb_block_call(obj, id_each, 0, 0, count_iter_i, (VALUE)&n);
return INT2NUM(n);
}
else {
VALUE v;
rb_scan_args(argc, argv, "1", &v);
return Qnil; /* not reached */
}
}