pulling random numbers from an array or hash

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.
I really wonder why they actually did pass by reference as default opposed to pass by value...
I really wonder why they actually did pass by reference as default opposed to pass by value...
http://programmers.stackexchange.com/questions/57068/why-are-objects-passed-by-reference


I couldn't find any discussions on design-decisions behind ruby but I imagine you can generalize pass-by-reference across multiple languages.
Performance and 'hiding' the fact that Ruby uses pointers indeed would be the first 2 that would come to my mind as well. I remember in XNA (which prefers pass by value most of the time) passing so many of my variables as a pointer manually. The way Ruby handles this is so much better for games. But it all depends in on your platform. For generic Windows applications "pass by value" by default has my personal preference and it has usually 0 performance impact as well in these cases.
I see... Maybe I just didn't realized that it's actually the default for most languages I've written on... coz now that I think of it, yeah they pass by reference too... 
Thank you very much for clarifying that for me Napoleon.

Original thread: https://forums.rpgmakerweb.com/threads/pulling-random-numbers-from-an-array-or-hash.17601/