Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created October 19, 2011 05:05
Show Gist options
  • Save burtlo/1297511 to your computer and use it in GitHub Desktop.
Save burtlo/1297511 to your computer and use it in GitHub Desktop.
first_array = [ 2, 1, 3]
second_array = first_array
second_array.sort!
puts first_array # [ 1, 2, 3 ]
@sagevann
Copy link

I ran this by another rubyist out there and my misunderstanding came from the fact that the behavior of the = keyword. My expectation was that if you assign a value to second_array, at line 4 above, because it's all by reference it would change the value referenced by first_array as well. In fact, the = operator appears to create a new pointer and changes the reference of second_array from first_array to whatever you're now pointing it to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment