diff options
| author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2024-02-20 19:34:50 +0900 |
|---|---|---|
| committer | Kevin Newton <kddnewton@gmail.com> | 2026-02-15 14:12:15 -0500 |
| commit | 116d402067c1d01ed67b16ec93fb523bd5109d07 (patch) | |
| tree | 9ed0f81052e64934218586ae691dd4d2d1fbec8d /spec/ruby | |
| parent | 2065b55980a0fc6386d58e4ede37d60c50e5b62f (diff) | |
[Feature #19979] Method definition with `&nil`
Allow methods to declare that they don't accept a block via `&nil`.
Diffstat (limited to 'spec/ruby')
| -rw-r--r-- | spec/ruby/language/block_spec.rb | 11 | ||||
| -rw-r--r-- | spec/ruby/language/method_spec.rb | 12 |
2 files changed, 23 insertions, 0 deletions
diff --git a/spec/ruby/language/block_spec.rb b/spec/ruby/language/block_spec.rb index 67aad76c57..1890f0726a 100644 --- a/spec/ruby/language/block_spec.rb +++ b/spec/ruby/language/block_spec.rb @@ -1110,6 +1110,17 @@ describe "`it` calls without arguments in a block" do end end end + + ruby_version_is "3.4" do + it "works alongside disallowed block argument" do + no_block = eval <<-EOF + proc {|arg1, &nil| arg1} + EOF + + no_block.call(:a).should == :a + -> { no_block.call(:a) {} }.should raise_error(ArgumentError, 'no block accepted') + end + end end # Duplicates specs in language/it_parameter_spec.rb diff --git a/spec/ruby/language/method_spec.rb b/spec/ruby/language/method_spec.rb index 8f9f094fd8..d12fd71b21 100644 --- a/spec/ruby/language/method_spec.rb +++ b/spec/ruby/language/method_spec.rb @@ -1127,6 +1127,18 @@ describe "A method" do result = m(1, {foo: :bar}) result.should == [1, nil, nil, {foo: :bar}, nil, {}] end + + ruby_version_is "3.4" do + evaluate <<-ruby do + def m(a, &nil); a end; + ruby + + m(1).should == 1 + + -> { m(1) {} }.should raise_error(ArgumentError, 'no block accepted') + -> { m(1, &proc {}) }.should raise_error(ArgumentError, 'no block accepted') + end + end end context 'when passing an empty keyword splat to a method that does not accept keywords' do |
