I just feel in the right mood to tell something more than the previous commentators, because some time ago I had almost the same headache with all these materials related to the transmission-link. So:
Ruby variables are actually references to basic objects inside a Ruby virtual machine. They are passed to the function "by value", which means that when you execute function(arg) , function actually gets a copy of the reference to the memory object. Thus, function gets a valid object reference, which is arg and a copy of the arg link. When you perform some action on an object referenced by arg or copy, you successfully modify that object directly. (But when you work directly with links, everything happens only with links, so the copied link inside the function can even be deleted, which does not affect the original link or object. Consider this:
array_ref0 = [1,2,3]
This is because by saying array_ref0 = [1,2,3] you reassigned the reference to the new object, but array_ref1 still refers to the old object, which is alive, because> = 1 refers to it (not to mention GC here )
def g(arg); arg = nil; end g(array_ref0) p array_ref0
Hope that cleared up a bit.
I am not telling you that #dup is your array, because previous commentators have given you comprehensive practical answers.
source share