- Joined
- Dec 29, 2012
- Messages
- 869
- Reaction score
- 97
- First Language
- Dutch
- Primarily Uses
b = a does not copy anything other than a reference if a is an array or hash. You do not end up with 2 arrays! What b = a does is simply giving you 2 variables that are EXACTLY the same object/class instance. See it as 2 roads that both lead to Rome. If you destroy Rome via the left road then Rome will also be destroyed when you reach it using the right road. But if you use clone() instead then when you take the right road Rome would still be intact. Because it's a copy and the copy was not destroyed.
# Rome is some class with a reference to a neighborhood x.#------------------------------------------------------------------------a = Rome.new()b = aa.destroytravel_to(a) # omg Rome is gone!travel_to( # omg Rome is gone!travel_to(a.neighborhood_x) # omg the neighborhood is also gone!#------------------------------------------------------------------------a = Rome.new()b = a.clonea.destroytravel_to(a) # omg Rome is gone!travel_to( # Welcome to Rome!travel_to(b.neighborhood_x) # omg but the neighborhood is gone! # (not deep cloned)But yeah you still get your references in b after using b = a but keep in mind that this is not a 'copy' nor a 'clone' so even if you alter a reference from b, the reference in a will change as well because they are all the same.
Deep cloning (yes there are several cloning methods) however would also copy all references. But I don't think this functionality is build into Ruby by default but I'm not sure.
You might want to google "pass by reference" and "pass by value" and "cloning". You will run into problems sooner or later when scripting a lot if you don't, and finding the error is near impossible if you don't know this then. In Ruby most variables however are passed by reference.
# Rome is some class with a reference to a neighborhood x.#------------------------------------------------------------------------a = Rome.new()b = aa.destroytravel_to(a) # omg Rome is gone!travel_to( # omg Rome is gone!travel_to(a.neighborhood_x) # omg the neighborhood is also gone!#------------------------------------------------------------------------a = Rome.new()b = a.clonea.destroytravel_to(a) # omg Rome is gone!travel_to( # Welcome to Rome!travel_to(b.neighborhood_x) # omg but the neighborhood is gone! # (not deep cloned)But yeah you still get your references in b after using b = a but keep in mind that this is not a 'copy' nor a 'clone' so even if you alter a reference from b, the reference in a will change as well because they are all the same.
Deep cloning (yes there are several cloning methods) however would also copy all references. But I don't think this functionality is build into Ruby by default but I'm not sure.
You might want to google "pass by reference" and "pass by value" and "cloning". You will run into problems sooner or later when scripting a lot if you don't, and finding the error is near impossible if you don't know this then. In Ruby most variables however are passed by reference.
Last edited by a moderator:
