Skip to content

Commit d49a42c

Browse files
andrykonchineregon
authored andcommitted
[GR-45621] Add lambda keyword argument to Proc#parameters
PullRequest: truffleruby/4148
2 parents c9213bc + 56c727d commit d49a42c

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Compatibility:
4949
* Add `File.lutime` and `Pathname#lutime` methods (#3039, @andrykonchin).
5050
* Add a deprecation warning for `Encoding#replicate` (#3039, @patricklinpl, @manefz, @nirvdrum).
5151
* Change `UnboundMethod#{==,inspect}` to use the owner module rather than the origin (#3039, @rwstauner, @manefz, @patricklinpl)
52+
* Support `lambda` keyword argument in `Proc#parameters` (#3039, @thomasmarshall, @goyox86).
5253

5354
Performance:
5455

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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,13 @@ 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, boolean isLambda) {
212212
final ArgumentDescriptor[] argsDesc = proc.getArgumentDescriptors();
213-
final boolean isLambda = proc.type == ProcType.LAMBDA;
214213
return ArgumentDescriptorUtils
215214
.argumentDescriptorsToParameters(getLanguage(), getContext(), argsDesc, isLambda);
216215
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,14 @@ def <<(other)
128128
end
129129
end
130130
end
131+
132+
def parameters(lambda: nil)
133+
if Primitive.nil?(lambda)
134+
lambda = lambda?
135+
else
136+
lambda = Primitive.as_boolean(lambda)
137+
end
138+
139+
Primitive.proc_parameters(self, lambda)
140+
end
131141
end

test/mri/excludes/TestSocket_TCPSocket.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
exclude :test_inspect, "needs investigation"
33
exclude :test_recvfrom, "needs investigation"
44
exclude :test_initialize_resolv_timeout, "needs investigation"
5+
exclude :test_initialize_connect_timeout, "transient"

0 commit comments

Comments
 (0)