Skip to content

Commit b5a0c62

Browse files
committed
Add specs for how parameters with the name _ are treated
Parameter name `_` is special in that it can be used multiple times in a method or proc definition. The specs added make sure that the `Method#parameters` and `Proc#parameters` methods return the correct information for such cases.
1 parent ce18482 commit b5a0c62

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

spec/ruby/core/method/parameters_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def one_splat_one_block(*args, &block)
2020
local_is_not_parameter = {}
2121
end
2222

23+
def underscore_parameters(_, _, _ = 1, *_, _:, _: 2, **_, &_); end
24+
2325
define_method(:one_optional_defined_method) {|x = 1|}
2426
end
2527

@@ -251,6 +253,20 @@ def one_splat_one_block(*args, &block)
251253
m.method(:writer=).parameters.should == [[:req]]
252254
end
253255

256+
it "returns all parameters defined with the name _ as _" do
257+
m = MethodSpecs::Methods.instance_method(:underscore_parameters)
258+
m.parameters.should == [
259+
[:req, :_],
260+
[:req, :_],
261+
[:opt, :_],
262+
[:rest, :_],
263+
[:keyreq, :_],
264+
[:key, :_],
265+
[:keyrest, :_],
266+
[:block, :_]
267+
]
268+
end
269+
254270
it "returns [[:rest]] for core methods with variable-length argument lists" do
255271
# delete! takes rest args
256272
"foo".method(:delete!).parameters.should == [[:rest]]

spec/ruby/core/proc/parameters_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,30 @@
115115
local_is_not_parameter = {}
116116
end.parameters.should == [[:rest, :args], [:block, :blk]]
117117
end
118+
119+
it "returns all parameters defined with the name _ as _" do
120+
proc = proc {|_, _, _ = 1, *_, _:, _: 2, **_, &_| }
121+
proc.parameters.should == [
122+
[:opt, :_],
123+
[:opt, :_],
124+
[:opt, :_],
125+
[:rest, :_],
126+
[:keyreq, :_],
127+
[:key, :_],
128+
[:keyrest, :_],
129+
[:block, :_]
130+
]
131+
132+
lambda = ->(_, _, _ = 1, *_, _:, _: 2, **_, &_) {}
133+
lambda.parameters.should == [
134+
[:req, :_],
135+
[:req, :_],
136+
[:opt, :_],
137+
[:rest, :_],
138+
[:keyreq, :_],
139+
[:key, :_],
140+
[:keyrest, :_],
141+
[:block, :_]
142+
]
143+
end
118144
end

0 commit comments

Comments
 (0)