Project

General

Profile

Actions

Bug #14008

closed

Adding elements to array inside 2D array duplicates across all other arrays

Added by v0dro (Sameer Deshmukh) over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
[ruby-core:83237]

Description

Say I create a 2D array in Ruby using the Array.new syntax like this:
arr = Array.new 3, []

Adding elements to say, the 2nd array inside arr produces the following result:

arr = Array.new 3, []
arr[2] << 3
arr
# => [[3], [3], [3]]

Expected behavior

I think this should work exactly like what happens when initializing 2D arrays using the literal syntax:

arr = [[],[],[]] 
arr[2] << 3
arr
# => [[], [], [3]] 

Updated by v0dro (Sameer Deshmukh) over 6 years ago

I'm so sorry I had forgotten about Array gotchas. Please close this issue.

Actions #2

Updated by duerst (Martin Dürst) over 6 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0