Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions language/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
25 changes: 25 additions & 0 deletions language/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down