DEV Community

Discussion on: Pass by Reference, Pass by Value

Collapse
sizief profile image
Ali Deishidi

Hi Jeremy, nice post, thanks for sharing! I had challenges with this concept in ruby in that past, these two things help me to understand it better:
this discussion on SO: stackoverflow.com/questions/187211...

Ans also paying attention to new object creation when we add something to passed array VS we define new array inside the method. See the code:

def append(array)
  array = array + [1]
  # array << 1 # VS this one which create new array
  p array
  p 'after assign: '+array.object_id.to_s
end

array = [0]
p 'original: ' + array.object_id.to_s
append(array)
p 'after def: ' + array.object_id.to_s
p array