Using Two Array Indices : Two Array Indices « Array « Ruby






Using Two Array Indices


array = ["Hello", "there", "AAA", 1, 2, 3]
array[2, 1] = "pal"
p array
array = ["Hello", "there", "AAA", 1, 2, 3]
array[3, 0] = "pie"
p array
array = ["Now", "is", 1, 2, 3]
array[2, 0] = ["the", "time"]
p array
array = ["Hello", "there", "AAA", 1, 2, 3]
array2 = array[3, 3]
p array2

 








Related examples in the same category

1.In Ruby the first index is the start location and the second holds the count: array[start, count].
2.array[1, 2] references two array elements, starting with the element at index 1, and this statement replaces two elements in the array, not just one:
3.Assigning a value to array[3, 0] did not replace any element in the array; it inserted a new element starting at index 3 instead.
4.specify where to start in the array and how many elements you want
5.use a range:
6.Instead of [], you can also use the slice method, another alias