require 'deep_clone' original = [1, [2, 3]] deep_copy = original.deep_clone original[1][0] = 42 puts deep_copy[1][0] # Output: 2 (since deep_copy is an independent copy)a = { "x" => 0, "y" => { "z" => 0 } } #=> {"x"=>0, "y"=>{"z"=>0} # normal copy, copies reference of a. b = a b["y"]["z"] = 1 b # {"x"=>0, "y"=>{"z"=>1}} a # {"x"=>0, "y"=>{"z"=>1}} # shallow copy, just changes the parent object reference not the child once c = b.clone c["x"] = 1 c # => {"x"=>1, "y"=>{"z"=>2}} b # => {"x"=>0, "y"=>{"z"=>2}} c["y"]["z"] = 3 c # => {"x"=>1, "y"=>{"z"=>3}} b # => {"x"=>0, "y"=>{"z"=>3}} #deep copy, changes all the child object reference d = JSON.parse(c.to_json) #=> {"x"=>1, "y"=>{"z"=>3}} d["y"]["z"] = 5 c # => {"x"=>1, "y"=>{"z"=>3}} d # => {"x"=>1, "y"=>{"z"=>5}}food = %w( bread milk orange ) food.map(&:object_id) # [35401044, 35401020, 35400996] food.clone.map(&:object_id) # [35401044, 35401020, 35400996]