1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/ostruct] Fix dup/clone

This commit is contained in:
Marc-Andre Lafortune 2020-09-14 15:15:28 -04:00 committed by Marc-André Lafortune
parent 125605abd9
commit 60f5d38482
Notes: git 2020-09-15 05:11:07 +09:00
2 changed files with 9 additions and 3 deletions

View file

@ -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
#

View file

@ -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