Skip to content

Commit 05ae525

Browse files
authored
Merge pull request jruby#9211 from evaniainbrooks/9208-block-param-default
Fix block argument destructuring with optional parameters
2 parents e8fe40d + 4e2ce7d commit 05ae525

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ private static IRubyObject[] prepareBlockArgsInternal(ThreadContext context, Blo
21892189

21902190
// FIXME: For NORMAL/THREAD but it is unclear if we really need any keyword logic in here anymore.
21912191
org.jruby.runtime.Signature sig = block.getBody().getSignature();
2192-
if (!sig.isSpreadable()) return args;
2192+
if (!sig.isSpreadable() || (sig.opt() + sig.required() == 1 && !sig.hasRest())) return args;
21932193

21942194
// We get here only when we need both required and optional/rest args
21952195
// (keyword or non-keyword in either case).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public IRubyObject doYield(ThreadContext context, Block block, IRubyObject value
161161
IRubyObject[] args;
162162
if (value == null) { // no args case from BlockBody.yieldSpecific
163163
args = IRubyObject.NULL_ARRAY;
164-
} else if (!signature.isSpreadable()) {
164+
} else if (!signature.isSpreadable() || (signature.opt() + signature.required() == 1 && !signature.hasRest())) {
165165
args = new IRubyObject[] { value };
166166
} else {
167167
args = toAry(context, value);

spec/ruby/language/yield_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
it "passes a single, multi-value Array" do
4949
@y.s([1, 2, 3]) { |*a| a }.should == [[1, 2, 3]]
5050
end
51+
52+
describe "with optional argument" do
53+
it "does not destructure a single array argument" do
54+
@y.s([1, 2, 3]) { |a = 99| a }.should == [1, 2, 3]
55+
end
56+
end
5157
end
5258

5359
describe "yielding to a lambda" do

0 commit comments

Comments
 (0)