Skip to content

Commit 81cdfef

Browse files
committed
Add specs for splatting **nil in Hash literals
1 parent 1b415b3 commit 81cdfef

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

language/hash_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ def h.to_hash; {:b => 2, :c => 3}; end
149149
{a: 1, **h, c: 4}.should == {a: 1, b: 2, c: 4}
150150
end
151151

152+
ruby_version_is ""..."3.4" do
153+
it "expands nil using ** into {}" do
154+
h = nil
155+
-> { {a: 1, **h} }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
156+
157+
-> { {a: 1, **nil} }.should raise_error(TypeError, "no implicit conversion of nil into Hash")
158+
end
159+
end
160+
161+
ruby_version_is "3.4" do
162+
it "expands nil using ** into {}" do
163+
h = nil
164+
{**h}.should == {}
165+
{a: 1, **h}.should == {a: 1}
166+
167+
{**nil}.should == {}
168+
{a: 1, **nil}.should == {a: 1}
169+
end
170+
end
171+
152172
it "expands an '**{}' or '**obj' element with the last key/value pair taking precedence" do
153173
-> {
154174
@h = eval "{a: 1, **{a: 2, b: 3, c: 1}, c: 3}"

0 commit comments

Comments
 (0)