Skip to content

Commit dddc276

Browse files
Add lambda keyword argument to Proc#parameters
Co-authored-by: Jose Narvaez <jose.narvaez@shopify.com>
1 parent 90bfaa0 commit dddc276

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Compatibility:
4444
* Do not autosplat a proc that accepts a single positional argument and keywords (#3039, @andrykonchin).
4545
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
4646
* Handle either positional or keywords arguments by default in `Struct.new` (#3039, @rwstauner).
47+
* Support `lambda` keyword argument in `Proc#parameters` (#3039, @thomasmarshall, @goyox86).
4748

4849
Performance:
4950

spec/ruby/core/proc/parameters_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
it "regards named parameters in lambda as optional if lambda: false keyword used" do
3434
-> x { }.parameters(lambda: false).first.first.should == :opt
3535
end
36+
37+
it "regards named parameters in procs and lambdas as required if lambda keyword is truthy" do
38+
proc {|x| }.parameters(lambda: 123).first.first.should == :req
39+
-> x { }.parameters(lambda: 123).first.first.should == :req
40+
end
41+
42+
it "ignores the lambda keyword if it is nil" do
43+
proc {|x|}.parameters(lambda: nil).first.first.should == :opt
44+
-> x { }.parameters(lambda: nil).first.first.should == :req
45+
end
3646
end
3747

3848
it "regards optional keyword parameters in procs as optional" do
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
fails:Proc#parameters sets the first element of each sub-Array to :req for required argument if lambda keyword used
2-
fails:Proc#parameters regards named parameters in procs as required if lambda keyword used
3-
fails:Proc#parameters regards named parameters in lambda as optional if lambda: false keyword used
41
fails:Proc#parameters adds rest arg with name * for "star" argument
52
fails:Proc#parameters adds keyrest arg with ** as a name for "double star" argument
63
fails:Proc#parameters adds block arg with name & for anonymous block argument

src/main/java/org/truffleruby/core/proc/ProcNodes.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,16 @@ boolean lambda(RubyProc proc) {
203203

204204
}
205205

206-
@CoreMethod(names = "parameters")
207-
public abstract static class ParametersNode extends CoreMethodArrayArgumentsNode {
206+
@Primitive(name = "proc_parameters")
207+
public abstract static class ParametersNode extends PrimitiveArrayArgumentsNode {
208208

209209
@TruffleBoundary
210210
@Specialization
211-
RubyArray parameters(RubyProc proc) {
211+
RubyArray parameters(RubyProc proc, Object lambdaObject) {
212212
final ArgumentDescriptor[] argsDesc = proc.getArgumentDescriptors();
213-
final boolean isLambda = proc.type == ProcType.LAMBDA;
213+
final boolean isLambda = (lambdaObject == nil)
214+
? proc.type == ProcType.LAMBDA
215+
: !Boolean.FALSE.equals(lambdaObject);
214216
return ArgumentDescriptorUtils
215217
.argumentDescriptorsToParameters(getLanguage(), getContext(), argsDesc, isLambda);
216218
}

src/main/ruby/truffleruby/core/proc.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,8 @@ def <<(other)
128128
end
129129
end
130130
end
131+
132+
def parameters(lambda: nil)
133+
Primitive.proc_parameters(self, lambda)
134+
end
131135
end

0 commit comments

Comments
 (0)