Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5fc4742afcbc
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: bb8807fbc487
Choose a head ref
  • 2 commits
  • 3 files changed
  • 2 contributors

Commits on Nov 23, 2015

  1. Enumerable#chunk and Enumerable#slice_before no longer takes the init…

    …ial_state argument [Feature #10958]
    MSNexploder committed Nov 23, 2015
    Copy the full SHA
    0c93f88 View commit details
  2. Merge pull request #3490 from MSNexploder/feature_10958

    remove "initial_state" argument of Enumerable#{slice_before,chunk}
    headius committed Nov 23, 2015
    Copy the full SHA
    bb8807f View commit details
Showing with 8 additions and 65 deletions.
  1. +7 −23 core/src/main/java/org/jruby/RubyEnumerable.java
  2. +1 −10 core/src/main/ruby/jruby/kernel/enumerable.rb
  3. +0 −32 test/mri/ruby/test_enum.rb
30 changes: 7 additions & 23 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -1865,8 +1865,6 @@ public static IRubyObject chunk(ThreadContext context, IRubyObject self, final I
switch (Arity.checkArgumentCount(context.runtime, args, 0, 1)) {
case 0:
return chunk(context, self, block);
case 1:
return chunk(context, self, args[0], block);
default:
// should never be reached
throw context.runtime.newArgumentError(args.length, 0);
@@ -1875,19 +1873,13 @@ public static IRubyObject chunk(ThreadContext context, IRubyObject self, final I

@JRubyMethod
public static IRubyObject chunk(ThreadContext context, IRubyObject self, final Block block) {
return chunk(context, self, context.nil, block);
}

@JRubyMethod
public static IRubyObject chunk(ThreadContext context, IRubyObject self, final IRubyObject initialState, final Block block) {
if(!block.isGiven()) {
throw context.runtime.newArgumentError("no block given");
}

IRubyObject enumerator = context.runtime.getEnumerator().allocate();
enumerator.getInternalVariables().setInternalVariable("chunk_enumerable", self);
enumerator.getInternalVariables().setInternalVariable("chunk_categorize", RubyProc.newProc(context.runtime, block, block.type));
enumerator.getInternalVariables().setInternalVariable("chunk_initial_state", initialState);

Helpers.invoke(context, enumerator, "initialize",
CallBlock.newCallClosure(self, context.runtime.getEnumerable(), Signature.ONE_ARGUMENT,
@@ -1906,13 +1898,10 @@ public IRubyObject size(IRubyObject[] args) {

private static class ChunkArg {

private ChunkArg(final ThreadContext context, IRubyObject state) {
this.state = state;
private ChunkArg(final ThreadContext context) {
this.prev_elts = this.prev_value = context.nil;
}

final IRubyObject state;

IRubyObject prev_value;
IRubyObject prev_elts;

@@ -1932,9 +1921,8 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block)
InternalVariables variables = enumerator.getInternalVariables();
final IRubyObject enumerable = (IRubyObject) variables.getInternalVariable("chunk_enumerable");
final RubyProc categorize = (RubyProc) variables.getInternalVariable("chunk_categorize");
final IRubyObject state = (IRubyObject) variables.getInternalVariable("chunk_initial_state");
final IRubyObject yielder = packEnumValues(context, args);
final ChunkArg arg = new ChunkArg(context, (state.isNil() ? null : state.dup()));
final ChunkArg arg = new ChunkArg(context);

final RubySymbol alone = runtime.newSymbol("_alone");
final RubySymbol separator = runtime.newSymbol("_separator");
@@ -1943,16 +1931,12 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block)
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
final IRubyObject larg = packEnumValues(ctx, largs);
final IRubyObject v;
if ( arg.state == null ) {
if ( categorize.getBlock().getSignature().arityValue() == 1 ) {
// if chunk's categorize block has arity one, we pass it the packed args
v = categorize.callMethod(ctx, "call", larg);
} else {
// else we let it spread the args as it sees fit for its arity
v = categorize.callMethod(ctx, "call", largs);
}
if ( categorize.getBlock().getSignature().arityValue() == 1 ) {
// if chunk's categorize block has arity one, we pass it the packed args
v = categorize.callMethod(ctx, "call", larg);
} else {
v = categorize.callMethod(ctx, "call", new IRubyObject[]{ larg, arg.state });
// else we let it spread the args as it sees fit for its arity
v = categorize.callMethod(ctx, "call", largs);
}

if ( v == alone ) {
11 changes: 1 addition & 10 deletions core/src/main/ruby/jruby/kernel/enumerable.rb
Original file line number Diff line number Diff line change
@@ -4,11 +4,6 @@ def slice_before(filter = (no_filter = true; nil), &block)

state = nil

if block && !no_filter
initial_state = filter.dup
state = initial_state
end

Enumerator.new do |yielder|
ary = nil
each do |*elt|
@@ -17,11 +12,7 @@ def slice_before(filter = (no_filter = true; nil), &block)
end

if block
if no_filter
state = block.call elt
else
state = block.call elt, initial_state
end
state = block.call elt
else
state = (filter === elt)
end
32 changes: 0 additions & 32 deletions test/mri/ruby/test_enum.rb
Original file line number Diff line number Diff line change
@@ -465,22 +465,6 @@ def test_chunk
e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)

e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a)
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated.

hs = [{}]
e = [:foo].chunk(hs[0]) {|elt, h|
hs << h
true
}
assert_equal([[true, [:foo]]], e.to_a)
assert_equal([[true, [:foo]]], e.to_a)
assert_equal([{}, {}, {}], hs)
assert_not_same(hs[0], hs[1])
assert_not_same(hs[0], hs[2])
assert_not_same(hs[1], hs[2])

e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
assert_equal([[:_alone, [1]],
[:_alone, [2]],
@@ -510,22 +494,6 @@ def test_slice_before
e = @obj.slice_before {|elt| elt.odd? }
assert_equal([[1,2], [3], [1,2]], e.to_a)

e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
assert_equal([[1,2], [3,1,2]], e.to_a)
assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated.

hs = [{}]
e = [:foo].slice_before(hs[0]) {|elt, h|
hs << h
true
}
assert_equal([[:foo]], e.to_a)
assert_equal([[:foo]], e.to_a)
assert_equal([{}, {}, {}], hs)
assert_not_same(hs[0], hs[1])
assert_not_same(hs[0], hs[2])
assert_not_same(hs[1], hs[2])

ss = %w[abc defg h ijk l mno pqr st u vw xy z]
assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
ss.slice_before(/\A...\z/).to_a)