DEV Community

Discussion on: Pass by Reference, Pass by Value

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:

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

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