Skip to content

Commit efdb8a9

Browse files
committed
Fix auto-splatting when passing methods as blocks
This fixes issue #5896 by always splatting out the arguments if possible.
1 parent 2d12159 commit efdb8a9

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

core/src/main/java/org/jruby/runtime/Helpers.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,15 +2439,17 @@ public static RubyString appendAsString(RubyString target, IRubyObject other) {
24392439
// . Array with multiple values and NO rest should extract args if there are more than one argument
24402440

24412441
static IRubyObject[] restructureBlockArgs(ThreadContext context,
2442-
IRubyObject value, Signature signature, Block.Type type, boolean needsSplat) {
2442+
IRubyObject value, Signature signature, Block.Type type) {
24432443

24442444
if (!type.checkArity && signature == Signature.NO_ARGUMENTS) return IRubyObject.NULL_ARRAY;
24452445

24462446
if (value == null) return IRubyObject.NULL_ARRAY;
24472447

2448-
if (needsSplat) {
2448+
if (signature != Signature.ONE_ARGUMENT) {
24492449
IRubyObject ary = Helpers.aryToAry(context, value);
2450-
if (ary instanceof RubyArray) return ((RubyArray) ary).toJavaArrayMaybeUnsafe();
2450+
if (ary instanceof RubyArray) {
2451+
return ((RubyArray) ary).toJavaArrayMaybeUnsafe();
2452+
}
24512453
}
24522454

24532455
return new IRubyObject[] { value };

core/src/main/java/org/jruby/runtime/MethodBlockBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public IRubyObject call(ThreadContext context, Block block, IRubyObject[] args,
6464

6565
@Override
6666
protected IRubyObject doYield(ThreadContext context, Block block, IRubyObject value) {
67-
IRubyObject[] realArgs = Helpers.restructureBlockArgs(context, value, getSignature(), block.type, false);
67+
IRubyObject[] realArgs = Helpers.restructureBlockArgs(context, value, getSignature(), block.type);
6868
return entry.method.call(context, receiver, entry.sourceModule, originName, realArgs, Block.NULL_BLOCK);
6969
}
7070

test/jruby/test_hash.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,25 @@ def test_compare_by_identity
108108
assert_equal 2, hash[arr2]
109109
end
110110

111+
def one_arg_method(a)
112+
a
113+
end
114+
115+
def two_arg_method(a, b)
116+
[a, b]
117+
end
118+
119+
def test_each_yield_to_method
120+
hsh = { a: 1, b: 2}
121+
hsh.each { |a| a }
122+
hsh.each { |a, b| [a, b]}
123+
124+
one_arg_lambda = lambda { |a| a }
125+
two_arg_lambda = lambda { |a, b| [a, b] }
126+
hsh.each(&one_arg_lambda)
127+
hsh.each(&two_arg_lambda)
128+
129+
hsh.each(&method(:one_arg_method))
130+
hsh.each(&method(:two_arg_method))
131+
end
111132
end

0 commit comments

Comments
 (0)