I want to create a destructive function. Let's consider the following example:
def not_really_destructive_fun(arr)
arr = [1,1,1]
puts "arr changed to: " + arr.to_s
end
def destructive_fun(arr)
p "Destructive function run."
arr.map!{|x| x+= 1}
end
Output of calling such methods is the following:
a = [1,2,3]
not_really_destructive_fun a # => arr changed to: [1, 1, 1]
a # => [1, 2, 3]
destructive_fun a # => "Destructive function run."
a # => [2, 3, 4]
map!
still changed the original value. However, =
didn't. How can I write a destructive function?