Skip to content

Commit 650eb28

Browse files
9032 Fix Different arguments for each.map vs each and map alone
1 parent 301c58f commit 650eb28

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

core/src/main/java/org/jruby/RubyEnumerator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,14 @@ private IRubyObject __each__(ThreadContext context, final Block block) {
324324
if (methodArgsHasKeywords) context.callInfo = CALL_KEYWORD;
325325
return object.callMethod(context, method, methodArgs,
326326
CallBlock.newCallClosure(context, this, Signature.OPTIONAL, (ctx, args, blk) -> {
327-
IRubyObject ret = block.yieldValues(ctx, args);
327+
IRubyObject ret;
328+
329+
if (args.length == 1) {
330+
ret = block.yield(ctx, args[0]);
331+
} else {
332+
ret = block.yieldValues(ctx, args);
333+
}
334+
328335
IRubyObject val = feedValue.use_value(ctx);
329336
return val.isNil() ? ret : val;
330337
})

spec/ruby/core/enumerator/each_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,19 @@ def object_each_with_arguments.each_with_arguments(arg, *args)
8686
ret.should be_an_instance_of(Enumerator)
8787
ret.should_not equal(@enum_with_arguments)
8888
end
89+
90+
it "does not destructure yielded array values when chaining each.map" do
91+
result = [[[1]]].each.map { |a, b| [a, b] }
92+
result.should == [[[1], nil]]
93+
end
94+
95+
it "preserves array values yielded from the enumerator" do
96+
result = [[1, 2]].each.map { |a| a }
97+
result.should == [[1, 2]]
98+
end
99+
100+
it "allows destructuring to occur in the block, not the enumerator" do
101+
result = [[1, 2]].each.map { |a, b| a }
102+
result.should == [1]
103+
end
89104
end

0 commit comments

Comments
 (0)