【戻る】 【進む】   目次        34. 組み込みクラス - Array

# 配列（Array）

```インデックス式
array = [2, 4, 6]
array0 = ["apple", Time.new, array]  #=> ["apple", Time.new, [2, 4, 6]]
array[2]     #=> 6
array0[0]    #=> "apple"
array0[2][2] #=> array[2] #=> 6
インデックス代入
array = [2, 4, 6]
array[1] = 8 #=> arrayは[2, 8, 6]となる
push,pop,shift,unshift
array = [1, 2, 3]に対する操作  操作後のarrayの値  式の値
array.push(4)                  [1, 2, 3, 4]       [1, 2, 3, 4]
array.unshift(0)               [0, 1, 2, 3]       [0, 1, 2, 3]

array = [1, 2, 3]に対する操作  操作後のarrayの値   式の値
array.pop                      [1, 2]              3
array.shift                    [2, 3]              1

array = [0, 1, 2, 3, 4, 5]
array[1, 3]   #=> [1, 2, 3]
array[-5, 3]  #=> [1, 2, 3]
array[1, 0]   #=> []

array = [0, 1, 2, 3, 4, 5] に対する操作 操作後のarrayの値
array[1, 2] = [10, 20, 30]              [0, 10, 20, 30, 3, 4, 5]
array[1, 2] = []                        [0, 3, 4, 5]
array[1, 0] = [10, 20]                  [0, 10, 20, 1, 2, 3, 4, 5]

array = [0, 1, 2, 3, 4, 5]
array[1..3]   #=> [1, 2, 3]
array[-5..3]  #=> [1, 2, 3]
array[1..-3]  #=> [1, 2, 3]
array[1..0]   #=> []
array[3..1]   #=> nil
array[2...4]  #=> [2, 3]

array = [0, 1, 2, 3, 4, 5]に対する操作  操作後のarrayの値
array[1..2] = [10, 20, 30]              [0, 10, 20, 30, 3, 4, 5]
array[1..2] = []                        [0, 3, 4, 5]
array[1..0] = [10, 20]                  [0, 10, 20, 1, 2, 3, 4, 5]

[0, 1, 2] + [3, 4] #=> [0, 1, 2, 3, 4]
[0, 1, 2] * 3 #=> [0, 1, 2, 0, 1, 2, 0, 1, 2]

array = [0, 1, 2]
array << 3        #=> [0, 1, 2, 3]
array << [4] << 5 #=> [0, 1, 2, 3, [4], 5]
array.concat([6]) #=> [0, 1, 2, 3, [4], 5, 6]

["banana", 0, "kiwi", {}, "kiwi"].index("kiwi") #=> 2
["banana", 0, "kiwi"].include?("orange") #=> false

array = ["a", "b", "c", "d", "e"]
array.indexes(1, 4, 3) #=> ["b", "e", "d"]
```

【戻る】 【進む】   目次        34. 組み込みクラス - Array