Skip to content

Instantly share code, notes, and snippets.

@ayumin
Created December 12, 2012 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayumin/4268613 to your computer and use it in GitHub Desktop.
Save ayumin/4268613 to your computer and use it in GitHub Desktop.
class Hash
def deep_clone
Marshal.load(Marshal.dump(self))
end
end
h = {} #=> {}
a = {'a' => 1, 'b' => 2, 'c' => h} #=> {"a"=>1, "b"=>2, "c"=>{}}
a1 = a.clone #=> {"a"=>1, "b"=>2, "c"=>{}}
a2 = a.deep_clone #=> {"a"=>1, "b"=>2, "c"=>{}}
h['foo'] = 'bar' #=> "bar"
a #=> {"a"=>1, "b"=>2, "c"=>{"foo"=>"bar"}}
a1 #=> {"a"=>1, "b"=>2, "c"=>{"foo"=>"bar"}}
a2 #=> {"a"=>1, "b"=>2, "c"=>{}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment