Skip to content

Commit 422819c

Browse files
nobuandrykonchin
authored andcommitted
[Feature #19979] Method definition with &nil
Allow methods to declare that they don't accept a block via `&nil`.
1 parent 0557d87 commit 422819c

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

language/block_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,17 @@ def o.it
11101110
end
11111111
end
11121112
end
1113+
1114+
ruby_version_is "3.4" do
1115+
it "works alongside disallowed block argument" do
1116+
no_block = eval <<-EOF
1117+
proc {|arg1, &nil| arg1}
1118+
EOF
1119+
1120+
no_block.call(:a).should == :a
1121+
-> { no_block.call(:a) {} }.should raise_error(ArgumentError, 'no block accepted')
1122+
end
1123+
end
11131124
end
11141125

11151126
# Duplicates specs in language/it_parameter_spec.rb

language/method_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,18 @@ def m(a, b = nil, c = nil, d, e: nil, **f)
11271127
result = m(1, {foo: :bar})
11281128
result.should == [1, nil, nil, {foo: :bar}, nil, {}]
11291129
end
1130+
1131+
ruby_version_is "3.4" do
1132+
evaluate <<-ruby do
1133+
def m(a, &nil); a end;
1134+
ruby
1135+
1136+
m(1).should == 1
1137+
1138+
-> { m(1) {} }.should raise_error(ArgumentError, 'no block accepted')
1139+
-> { m(1, &proc {}) }.should raise_error(ArgumentError, 'no block accepted')
1140+
end
1141+
end
11301142
end
11311143

11321144
context 'when passing an empty keyword splat to a method that does not accept keywords' do

0 commit comments

Comments
 (0)