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: 83001bea08f8
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 067cad1649ed
Choose a head ref
  • 7 commits
  • 2 files changed
  • 2 contributors

Commits on Nov 15, 2016

  1. add Enumerable#sum

    JRuby #4293
    * feature#12217
    phluid61 committed Nov 15, 2016
    Copy the full SHA
    cd79736 View commit details
  2. Copy the full SHA
    a6651aa View commit details
  3. add Array#sum

    JRuby #4293
    * feature#12217
    phluid61 committed Nov 15, 2016
    Copy the full SHA
    62861d6 View commit details
  4. Copy the full SHA
    1452408 View commit details
  5. Copy the full SHA
    0795e08 View commit details
  6. Copy the full SHA
    93d8b8c View commit details
  7. Merge pull request #4297 from phluid61/feature/12217-enumerable-sum

    Feature#12217 Enumerable/Array sum
    enebo authored Nov 15, 2016
    Copy the full SHA
    067cad1 View commit details
Showing with 80 additions and 0 deletions.
  1. +38 −0 core/src/main/java/org/jruby/RubyArray.java
  2. +42 −0 core/src/main/java/org/jruby/RubyEnumerable.java
38 changes: 38 additions & 0 deletions core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
@@ -4310,6 +4310,44 @@ private IRubyObject any_pBlockless(ThreadContext context) {
return context.runtime.getFalse();
}

@JRubyMethod
public IRubyObject sum(final ThreadContext context, final Block block) {
final Ruby runtime = context.runtime;
RubyFixnum zero = RubyFixnum.zero(runtime);

if (!isBuiltin("each")) return RubyEnumerable.sumCommon(context, this, zero, block);

return sumCommon(context, zero, block);
}

@JRubyMethod
public IRubyObject sum(final ThreadContext context, IRubyObject init, final Block block) {
if (!isBuiltin("each")) return RubyEnumerable.sumCommon(context, this, init, block);

return sumCommon(context, init, block);
}

/* FIXME: optimise for special types (e.g. Integer)? */
/* NB: MRI says "Array#sum method may not respect method redefinition of "+" methods such as Integer#+." */
public IRubyObject sumCommon(final ThreadContext context, IRubyObject init, final Block block) {
final Ruby runtime = context.runtime;
final IRubyObject result[] = new IRubyObject[] { init };

if (block.isGiven()) {
for (int i = 0; i < realLength; i++) {
IRubyObject value = block.yield(context, eltOk(i));
result[0] = result[0].callMethod(context, "+", value);
}
} else {
for (int i = 0; i < realLength; i++) {
IRubyObject value = eltOk(i);
result[0] = result[0].callMethod(context, "+", value);
}
}

return result[0];
}

public IRubyObject find(ThreadContext context, IRubyObject ifnone, Block block) {
if (!isBuiltin("each")) return RubyEnumerable.detectCommon(context, this, block);

42 changes: 42 additions & 0 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -907,6 +907,48 @@ public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
}
}

@JRubyMethod
public static IRubyObject sum(ThreadContext context, IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
RubyFixnum zero = RubyFixnum.zero(runtime);
return sumCommon(context, self, zero, block);
}

@JRubyMethod
public static IRubyObject sum(ThreadContext context, IRubyObject self, IRubyObject init, final Block block) {
return sumCommon(context, self, init, block);
}

/* FIXME: optimise for special types (e.g. Integer)? */
/* NB: MRI says "Enumerable#sum method may not respect method redefinition of "+" methods such as Integer#+." */
public static IRubyObject sumCommon(final ThreadContext context, IRubyObject self, IRubyObject init, final Block block) {
final Ruby runtime = context.runtime;
final IRubyObject result[] = new IRubyObject[] { init };

if (block.isGiven()) {
callEach(runtime, context, self, Signature.OPTIONAL, new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = packEnumValues(ctx, largs);
IRubyObject val = block.yieldArray(ctx, larg, null);
result[0] = result[0].callMethod(context, "+", val);

return ctx.nil;
}
});
} else {
callEach(runtime, context, self, Signature.OPTIONAL, new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = packEnumValues(ctx, largs);
result[0] = result[0].callMethod(context, "+", larg);

return ctx.nil;
}
});
}

return result[0];
}

public static IRubyObject injectCommon(final ThreadContext context, IRubyObject self, IRubyObject init, final Block block) {
final Ruby runtime = context.runtime;
final IRubyObject result[] = new IRubyObject[] { init };