Anonymous user
Creating an Array: Difference between revisions
→Ruby
m (→Tcl: fix lang tag) |
(→Ruby) |
||
Line 818:
<lang ruby>a = [[0] * 3] * 2] # [[0, 0, 0], [0, 0, 0]]
a[0][0] = 1
So both inner arrays refer to the same object
<lang ruby>a[0].equal? a[1] # true</lang>
The better way to create a multidimensional array is to use the form of <tt>Array.new</tt> that takes a block:
<lang ruby>a = Array.new(2) {Array.new(3) {0}}
a[1][1] = 1
You can also create a sequential array from a range using the 'splat' operator:
|