Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created April 13, 2012 19:36
Show Gist options
  • Save makaroni4/2379517 to your computer and use it in GitHub Desktop.
Save makaroni4/2379517 to your computer and use it in GitHub Desktop.
Different ways to copy object in Ruby
h = {}
h[:label] = "Red"
h_copy = h
h_dup = h.dup
h_clone = h.clone
h_deep_copy = Marshal.load( Marshal.dump h )
p h[:label].object_id # => 2156268500
p h_dup[:label].object_id # => 2156268500
p h_clone[:label].object_id # => 2156268500
p h_copy[:label].object_id # => 2156268500
p h_deep_copy[:label].object_id # => 2156268340
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment