Skip to content

Commit

Permalink
Don't use #size for #count in 1.9+
Browse files Browse the repository at this point in the history
Starting in 1.9, #count is always computed through enumeration
  • Loading branch information
dmarcotte committed Aug 30, 2013
1 parent 4e459d8 commit e542157
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Expand Up @@ -121,7 +121,16 @@ private static void checkContext(ThreadContext firstContext, ThreadContext secon
}
}

@JRubyMethod
@JRubyMethod(name = "count", compat = CompatVersion.RUBY1_8)
public static IRubyObject count18(ThreadContext context, IRubyObject self, final Block block) {
if (!block.isGiven() && self.respondsTo("size")) {
return self.callMethod(context, "size");
}

return count(context, self, block);
}

@JRubyMethod(name = "count", compat = CompatVersion.RUBY1_9)
public static IRubyObject count(ThreadContext context, IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
final int result[] = new int[] { 0 };
Expand All @@ -134,8 +143,6 @@ public IRubyObject yield(ThreadContext context, IRubyObject arg) {
}
});
} else {
if (self.respondsTo("size")) return self.callMethod(context, "size");

each(context, self, new JavaInternalBlockBody(runtime, context, "Enumerable#count", Arity.NO_ARGUMENTS) {
public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
result[0]++;
Expand All @@ -145,8 +152,13 @@ public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
}
return RubyFixnum.newFixnum(runtime, result[0]);
}

@JRubyMethod

@JRubyMethod(name = "count", compat = CompatVersion.RUBY1_8)
public static IRubyObject count18(ThreadContext context, IRubyObject self, final IRubyObject methodArg, final Block block) {
return count(context, self, methodArg, block);
}

@JRubyMethod(name = "count", compat = CompatVersion.RUBY1_9)
public static IRubyObject count(ThreadContext context, IRubyObject self, final IRubyObject methodArg, final Block block) {
final Ruby runtime = context.runtime;
final int result[] = new int[] { 0 };
Expand Down

0 comments on commit e542157

Please sign in to comment.