Skip to content

Commit

Permalink
Merge pull request #4951 from yui-knk/test_reject
Browse files Browse the repository at this point in the history
Fix `Hash#reject`
  • Loading branch information
enebo committed Jan 8, 2018
2 parents ab75bbb + 5def73d commit 9504a1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
20 changes: 19 additions & 1 deletion core/src/main/java/org/jruby/RubyHash.java
Expand Up @@ -1694,11 +1694,29 @@ public IRubyObject delete_if(final ThreadContext context, final Block block) {
return block.isGiven() ? delete_ifInternal(context, block) : enumeratorizeWithSize(context, this, "delete_if", enumSizeFn());
}

private static final class RejectVisitor extends VisitorWithState<Block> {
final RubyHash result;
RejectVisitor(RubyHash result) {
this.result = result;
}

@Override
public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, Block block) {
if (!block.yieldArray(context, RubyArray.newArray(context.runtime, key, value), null).isTrue()) {
result.fastASet(key, value);
}
}
}

/** rb_hash_reject
*
*/
public RubyHash rejectInternal(ThreadContext context, Block block) {
return ((RubyHash)dup()).delete_ifInternal(context, block);
final RubyHash result = newHash(context.runtime);

iteratorVisitAll(context, new RejectVisitor(result), block);

return result;
}

@JRubyMethod
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestHash.rb
Expand Up @@ -10,7 +10,6 @@
exclude :test_inverse_hash, "needs investigation"
exclude :test_NEWHASH_fstring_key, "needs investigation"
exclude :test_recursive_hash_value_struct, "needs investigation"
exclude :test_reject, "needs investigation"
exclude :test_AREF_fstring_key, "Depends on MRI-specific GC.stat key"
exclude :test_ASET_fstring_key, "due https://github.com/jruby/jruby/commit/f3f0091da7d98c5df285"

Expand Down

0 comments on commit 9504a1c

Please sign in to comment.