diff --git a/spec/ruby/core/struct/new_spec.rb b/spec/ruby/core/struct/new_spec.rb index f8881069504e..691307519856 100644 --- a/spec/ruby/core/struct/new_spec.rb +++ b/spec/ruby/core/struct/new_spec.rb @@ -74,6 +74,10 @@ def obj.to_str() "Foo" end end end + it "raises ArgumentError when there is a duplicate member" do + -> { Struct.new(:foo, :foo) }.should raise_error(ArgumentError, "duplicate member: foo") + end + it "raises a TypeError if object is not a Symbol" do obj = mock(':ruby') def obj.to_sym() :ruby end @@ -156,6 +160,10 @@ def platform obj.legs.should == 4 end + it "raises when there is a duplicate member" do + -> { Struct.new(:foo, :foo, keyword_init: true) }.should raise_error(ArgumentError, "duplicate member: foo") + end + describe "new class instantiation" do it "accepts arguments as hash as well" do obj = @struct_with_kwa.new({name: "elefant", legs: 4}) diff --git a/src/main/ruby/truffleruby/core/struct.rb b/src/main/ruby/truffleruby/core/struct.rb index ebcd262db5d7..d375db5ede8d 100644 --- a/src/main/ruby/truffleruby/core/struct.rb +++ b/src/main/ruby/truffleruby/core/struct.rb @@ -63,6 +63,11 @@ def self.new(klass_name, *attrs, keyword_init: true, &block) end end + duplicates = attrs.uniq! + if duplicates + raise ArgumentError, "duplicate member: #{duplicates.first}" + end + klass = Class.new self do _specialize attrs