Skip to content

Commit 352a0cb

Browse files
committed
Fix Struct#initialize when mixed positional and keyword arguments
1 parent 093a315 commit 352a0cb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

core/struct/new_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,35 @@ def platform
164164
obj.args.should == 42
165165
obj2.args.should == 42
166166
end
167+
168+
context "given positional and keyword arguments" do
169+
it "treats keyword arguments as a positional parameter" do
170+
type = Struct.new(:a, :b)
171+
s = type.new("a", b: "b")
172+
s.a.should == "a"
173+
s.b.should == {b: "b"}
174+
175+
type = Struct.new(:a, :b, :c)
176+
s = type.new("a", b: "b", c: "c")
177+
s.a.should == "a"
178+
s.b.should == {b: "b", c: "c"}
179+
s.c.should == nil
180+
end
181+
182+
it "ignores empty keyword arguments" do
183+
type = Struct.new(:a, :b)
184+
h = {}
185+
s = type.new("a", **h)
186+
187+
s.a.should == "a"
188+
s.b.should == nil
189+
end
190+
191+
it "raises ArgumentError when all struct attribute values are specified" do
192+
type = Struct.new(:a, :b)
193+
-> { type.new("a", "b", c: "c") }.should raise_error(ArgumentError, "struct size differs")
194+
end
195+
end
167196
end
168197

169198
context "keyword_init: true option" do

0 commit comments

Comments
 (0)