public static IRubyObject count(ThreadContext context, IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
final int result[] = new int[] { 0 };
if (block.isGiven()) {
each(context, self, new JavaInternalBlockBody(context, "Enumerable#count", block.arity()) {
public IRubyObject yield(ThreadContext context, IRubyObject arg) {
if (block.yield(context, arg).isTrue()) result[0]++;
return runtime.getNil();
}
});
} else {
if (self.respondsTo("size")) return self.callMethod(context, "size");
each(context, self, new JavaInternalBlockBody(context, "Enumerable#count", Arity.NO_ARGUMENTS) {
public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
result[0]++;
return runtime.getNil();
}
});