Skip to content

Commit 7e99bdf

Browse files
committed
Follow MRI behavior for Hash#{each,each_pair}
* The behavior changes based on the block arity.
1 parent c74972d commit 7e99bdf

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/main/java/org/truffleruby/core/hash/HashNodes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ protected boolean equalKeys(boolean compareByIdentity, Object key, int hashed, O
427427
@ImportStatic(HashGuards.class)
428428
public abstract static class EachNode extends YieldingCoreMethodNode {
429429

430+
private final ConditionProfile arityMoreThanOne = ConditionProfile.createBinaryProfile();
431+
430432
@Specialization(guards = "isNullHash(hash)")
431433
public DynamicObject eachNull(DynamicObject hash, DynamicObject block) {
432434
return hash;
@@ -475,7 +477,12 @@ public DynamicObject eachBuckets(DynamicObject hash, DynamicObject block) {
475477
}
476478

477479
private Object yieldPair(DynamicObject block, Object key, Object value) {
478-
return yield(block, createArray(new Object[]{key, value}, 2));
480+
// MRI behavior, see rb_hash_each_pair()
481+
if (arityMoreThanOne.profile(Layouts.PROC.getSharedMethodInfo(block).getArity().getArityNumber() > 1)) {
482+
return yield(block, key, value);
483+
} else {
484+
return yield(block, createArray(new Object[]{key, value}, 2));
485+
}
479486
}
480487

481488
}

0 commit comments

Comments
 (0)