ruby-changes:62958
From: Marc-Andre <ko1@a...>
Date: Tue, 15 Sep 2020 05:11:01 +0900 (JST)
Subject: [ruby-changes:62958] 60f5d38482 (master): [ruby/ostruct] Fix dup/clone
https://git.ruby-lang.org/ruby.git/commit/?id=60f5d38482 From 60f5d384820a4b07f739d32c2233b1dbc74a726a Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune <github@m...> Date: Mon, 14 Sep 2020 15:15:28 -0400 Subject: [ruby/ostruct] Fix dup/clone diff --git a/lib/ostruct.rb b/lib/ostruct.rb index f403f63..6f8f255 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -130,10 +130,14 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L130 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 63767bb..c14a4ba 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/openstruct/frozen_spec.rb#L24 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 https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/openstruct/frozen_spec.rb#L32 it "creates an unfrozen dup" do d = @os.dup + d.frozen?.should == false d.age.should == 70 d.age = 42 d.age.should == 42 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/