Skip to content

Commit

Permalink
Showing 3 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/java/org/jruby/RubyProc.java
Original file line number Diff line number Diff line change
@@ -265,7 +265,16 @@ public static IRubyObject[] prepareArgs(ThreadContext context, Block.Type type,
// for procs and blocks, single array passed to multi-arg must be spread
if ((signature != Signature.ONE_ARGUMENT && required != 0 && (isFixed || signature != Signature.OPTIONAL) || restKwargs) &&
actual == 1 && args[0].respondsTo("to_ary")) {
args = args[0].convertToArray().toJavaArray();
IRubyObject newAry = Helpers.aryToAry(args[0]);

// This is very common to yield in *IRBlockBody. When we tackle call protocol for blocks this will combine.
if (newAry.isNil()) {
args = new IRubyObject[] { args[0] };
} else if (newAry instanceof RubyArray){
args = ((RubyArray) newAry).toJavaArray();
} else {
throw context.runtime.newTypeError(args[0].getType().getName() + "#to_ary should return Array");
}
actual = args.length;
}

5 changes: 5 additions & 0 deletions spec/ruby/core/proc/fixtures/common.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module ProcSpecs
class ToAryAsNil
def to_ary
nil
end
end
def self.new_proc_in_method
Proc.new
end
8 changes: 8 additions & 0 deletions spec/ruby/core/proc/shared/call.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require File.expand_path('../../fixtures/common', __FILE__)

describe :proc_call, shared: true do
it "invokes self" do
Proc.new { "test!" }.send(@method).should == "test!"
@@ -49,6 +51,12 @@
a.should == 1
end

it "will call #to_ary on argument and return self if return is nil" do
argument = ProcSpecs::ToAryAsNil.new
result = proc { |x, _| x }.send(@method, argument)
result.should == argument
end

it "substitutes nil for missing arguments when self is a proc" do
proc {|x,y| [x,y]}.send(@method).should == [nil,nil]

0 comments on commit 5f11f7e

Please sign in to comment.