From 60f5d384820a4b07f739d32c2233b1dbc74a726a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 14 Sep 2020 15:15:28 -0400 Subject: [PATCH] [ruby/ostruct] Fix dup/clone --- lib/ostruct.rb | 10 +++++++--- spec/ruby/library/openstruct/frozen_spec.rb | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ostruct.rb b/lib/ostruct.rb index f403f63bdb..6f8f255511 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -130,10 +130,14 @@ class OpenStruct end # Duplicates an OpenStruct object's Hash table. - def initialize_copy(orig) # :nodoc: - orig.table.each_key{|key| new_ostruct_member!(key)} + private def initialize_clone(orig) # :nodoc: + super # clones the singleton class for us + @table = @table.dup unless @table.frozen? + end + + private def initialize_dup(orig) # :nodoc: super - @table = @table.dup + initialize(@table) end # diff --git a/spec/ruby/library/openstruct/frozen_spec.rb b/spec/ruby/library/openstruct/frozen_spec.rb index 63767bb54d..c14a4bac55 100644 --- a/spec/ruby/library/openstruct/frozen_spec.rb +++ b/spec/ruby/library/openstruct/frozen_spec.rb @@ -24,6 +24,7 @@ describe "OpenStruct.new when frozen" do it "creates a frozen clone" do f = @os.clone + f.frozen?.should == true f.age.should == 70 ->{ f.age = 0 }.should raise_error( RuntimeError ) ->{ f.state = :newer }.should raise_error( RuntimeError ) @@ -31,6 +32,7 @@ describe "OpenStruct.new when frozen" do it "creates an unfrozen dup" do d = @os.dup + d.frozen?.should == false d.age.should == 70 d.age = 42 d.age.should == 42