count(p1)
public
Returns the number of items in enum through enumeration. If an
argument is given, the number of items in enum that are equal to
item are counted. If a block is given, it counts the number of
elements yielding a true value.
ary = [1, 2, 4, 2]
ary.count
ary.count(2)
ary.count{ |x| x%2==0 }
Show source
static VALUE
enum_count(int argc, VALUE *argv, VALUE obj)
{
VALUE item = Qnil;
struct MEMO *memo;
rb_block_call_func *func;
if (argc == 0) {
if (rb_block_given_p()) {
func = count_iter_i;
}
else {
func = count_all_i;
}
}
else {
rb_scan_args(argc, argv, "1", &item);
if (rb_block_given_p()) {
rb_warn("given block not used");
}
func = count_i;
}
memo = MEMO_NEW(item, 0, 0);
rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
return imemo_count_value(memo);
}