Consider the following code in ruby, assume I called prestart from somewhere:
def tester(process_name, *host_list)
hosts = []
hosts = host_list[0]
hosts[0] = nil
end
def prestart(process_name, *host)
host_list = ['192.168.1.1', '192.168.1.2']
puts host_list.inspect # -> ['192.168.1.1', '192.168.1.2']
tester(process_name, host_list)
puts host_list.inspect # -> [nil, '192.168.1.2']
abort
end
How did it become nil? Is this how ruby works? If yes, how do I make sure it doesn’t effect the caller?
Arrays are objects, and therefore are past by reference. If you want to change it without affecting the original, you need to duplicate it by calling
.dupon it. You can do it either in the caller or in the called method.