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: 5096fee63412
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 516792fca582
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Nov 26, 2015

  1. Copy the full SHA
    7d48171 View commit details
  2. Merge pull request #3498 from lucasallan/grep_v

    [Ruby-2.3] - Implements Enumerable#grep_v
    kares committed Nov 26, 2015
    Copy the full SHA
    516792f View commit details
Showing with 11 additions and 3 deletions.
  1. +11 −3 core/src/main/java/org/jruby/RubyEnumerable.java
14 changes: 11 additions & 3 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -539,14 +539,23 @@ public int compare(IRubyObject[] o1, IRubyObject[] o2) {

@JRubyMethod
public static IRubyObject grep(ThreadContext context, IRubyObject self, final IRubyObject pattern, final Block block) {
return grep(context, self, pattern, block, true);
}

@JRubyMethod(name = "grep_v")
public static IRubyObject inverseGrep(ThreadContext context, IRubyObject self, final IRubyObject pattern, final Block block) {
return grep(context, self, pattern, block, false);
}

private static IRubyObject grep(ThreadContext context, IRubyObject self, final IRubyObject pattern, final Block block, final boolean isPresent) {
final Ruby runtime = context.runtime;
final RubyArray result = runtime.newArray();

if (block.isGiven()) {
callEach(runtime, context, self, block.getSignature(), new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = packEnumValues(ctx, largs);
if (pattern.callMethod(ctx, "===", larg).isTrue()) {
if (pattern.callMethod(ctx, "===", larg).isTrue() == isPresent) {
IRubyObject value = block.yield(ctx, larg);
synchronized (result) {
result.append(value);
@@ -559,7 +568,7 @@ public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
callEach(runtime, context, self, Signature.ONE_REQUIRED, new BlockCallback() {
public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {
IRubyObject larg = packEnumValues(ctx, largs);
if (pattern.callMethod(ctx, "===", larg).isTrue()) {
if (pattern.callMethod(ctx, "===", larg).isTrue() == isPresent ) {
synchronized (result) {
result.append(larg);
}
@@ -571,7 +580,6 @@ public IRubyObject call(ThreadContext ctx, IRubyObject[] largs, Block blk) {

return result;
}

public static IRubyObject detectCommon(ThreadContext context, IRubyObject self, final Block block) {
return detectCommon(context, self, null, block);
}