Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -1214,16 +1214,25 @@ public IRubyObject call(ThreadContext ctx, IRubyObject larg, Block blk) {

@JRubyMethod
public IRubyObject count(ThreadContext context, Block block) {
if (isBeginless || isEndless) return asFloat(context, RubyFloat.INFINITY);
if (!block.isGiven()) {
if (isBeginless || isEndless) return asFloat(context, RubyFloat.INFINITY);

if (begin instanceof RubyInteger) {
IRubyObject size = size(context);
if (!size.isNil()) return size;
if (begin instanceof RubyInteger) {
IRubyObject size = size(context);
if (!size.isNil()) return size;
}

// fall back on Enumerable logic for other cases
}

return RubyEnumerable.count(context, this, block);
}

@JRubyMethod
public IRubyObject count(ThreadContext context, IRubyObject arg, Block block) {
return RubyEnumerable.count(context, this, arg, block);
}

@JRubyMethod
public IRubyObject minmax(ThreadContext context, Block block) {
if (block.isGiven()) return Helpers.invokeSuper(context, this, context.runtime.getRange(), "minmax", NULL_ARRAY, block);
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/core/range/count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@
(...nil).count.should == inf
(..10.0).count.should == inf
end

it "accepts an argument for comparison using ==" do
(1..10).count(2).should == 1
end

it "uses a block for comparison" do
(1..10).count{|x| x%2==0 }.should == 5
end

it "ignores the block when given an argument" do
-> {
(1..10).count(4){|x| x%2==0 }.should == 1
}.should complain(/given block not used/)
end
end
Loading