From 0762008a23905f19f1768fe6feec1e5c1b929de3 Mon Sep 17 00:00:00 2001 From: Volodymyr Shabanytsia Date: Fri, 4 Jul 2025 20:20:16 +0300 Subject: [PATCH 1/2] Add specs for splatting **nil in Hash literals --- language/hash_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/language/hash_spec.rb b/language/hash_spec.rb index b119b6ca73..668716e2e3 100644 --- a/language/hash_spec.rb +++ b/language/hash_spec.rb @@ -149,6 +149,26 @@ def h.to_hash; {:b => 2, :c => 3}; end {a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4} end + ruby_version_is ""..."3.4" do + it "does not expand nil using ** into {} and raises TypeError" do + h = nil + -> { {a: 1, **h} }.should raise_error(TypeError, "no implicit conversion of nil into Hash") + + -> { {a: 1, **nil} }.should raise_error(TypeError, "no implicit conversion of nil into Hash") + end + end + + ruby_version_is "3.4" do + it "expands nil using ** into {}" do + h = nil + {**h}.should == {} + {a: 1, **h}.should == {a: 1} + + {**nil}.should == {} + {a: 1, **nil}.should == {a: 1} + end + end + it "expands an '**{}' or '**obj' element with the last key/value pair taking precedence" do -> { @h = eval "{a: 1, **{a: 2, b: 3, c: 1}, c: 3}" From 56448c5eb8cf91ab134d4a5b1a76f91ce2f9b95e Mon Sep 17 00:00:00 2001 From: Volodymyr Shabanytsia Date: Fri, 4 Jul 2025 20:20:39 +0300 Subject: [PATCH 2/2] Add specs for splatting **nil in method call arguments --- language/method_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/language/method_spec.rb b/language/method_spec.rb index b0d7058dbe..1d412a133e 100644 --- a/language/method_spec.rb +++ b/language/method_spec.rb @@ -1175,6 +1175,31 @@ def m(a = nil, b = {}, v: false) end end +context "when passing **nil into a method that accepts keyword arguments" do + ruby_version_is ""..."3.4" do + it "raises TypeError" do + def m(**kw) kw; end + + h = nil + -> { m(a: 1, **h) }.should raise_error(TypeError, "no implicit conversion of nil into Hash") + -> { m(a: 1, **nil) }.should raise_error(TypeError, "no implicit conversion of nil into Hash") + end + end + + ruby_version_is "3.4" do + it "expands nil using ** into {}" do + def m(**kw) kw; end + + h = nil + m(**h).should == {} + m(a: 1, **h).should == {a: 1} + + m(**nil).should == {} + m(a: 1, **nil).should == {a: 1} + end + end +end + describe "A method call with a space between method name and parentheses" do before(:each) do def m(*args)