Array assign vs. append behavior

ruby

Solution

You redefine local variable in the first method.

This is the same as

visited = []
local_visited = visited
local_visited = ['A']
visited
# => [] 

And in the second method:

visited = []
local_visited = visited
local_visited << 'A'
visited
# => ["A"] 

Problem

The following behavior looks to me like the `assign` method is processing `visited` by value, whereas the `append` method is treating it as a reference: ``` class MyClass def assign(visited) visited += ["A"] end def append(visited) visited << "A" end end instance = MyClass.new visited = [] instance.assign(visited) visited # => [] instance.append(visited) visited # => ["A"] ``` Can someone explain this behavior? This is not a question about whether Ruby supports pass by reference or pass by value, but rather about the example provided below, and why two methods that purportedly do the same thing exhibit different behaviors.

Original source

Related problems