Ruby: Array

By Xah Lee. Date: . Last updated: .

Array is a ordered sequence of items. In ruby, each element can be of any Value Type, and you can add items or delete items in array. (it can grow or shrink)

Create Array

Create array using array literal expression.

# ruby

xx = [0, 1, 2, "a"]
p xx == [0, 1, 2, "a"]

Create array using Array function.

# ruby

# create a array of 3 elements, each is nil
bb = Array.new(3)
p bb == [nil, nil, nil]

# create array of 3 elements, each is 1
cc = Array.new(3,1)
p cc == [1, 1, 1]

Length (Count of Elements)

# ruby

xx = ["b", 4, 6]
p xx.length == 3

Get a Element

Array element can be extracted by appending a square bracket with index.

array[index]

# ruby

xx = ["a","b","c"]
p xx[0] == "a"
p xx[1] == "b"

Negative index counts from right.

# ruby

xx = ["a","b","c"]
p xx[-1] == "c"

Get Sub-Array (Slice)

Consecutive elements can be extracted using the form

array[startIndex, count]

# ruby

xx = [ "a", "b", "c", "d"]
p xx[1,2] == ["b", "c"]
# first is index, second is count

Change Element

A element can be changed with the form

array[index] = new_value

# ruby

xx = [0, 1, 2]
xx[2] = "b"
p xx == [0, 1, "b"]

A slice (continuous sequence) of elements can be changed by assigning to a array directly. The length of the slice need not match the length of new array.

# ruby

xx = [0,1,2,3,4]

# change 4 elements of array to a new array, starting at index 2
xx[2,4] = ["x", "y"]
p xx == [0, 1, "x", "y"]

Nested Array

Array can be nested arbitrarily. Append extra bracket to get element of nested array.

# ruby

xx = [3, 4, [7, 8]]
p xx[2][1] == 8

Add element

# ruby

xx = [0, 1, 2]

p xx.insert(1,"b") == [0, "b", 1, 2]

p xx == [0, "b", 1, 2]

Appending element

# ruby

# appending to array

xx = [0, 1]
xx << 2
p xx == [0, 1, 2]

xx << [5, 6]
p xx == [0, 1, 2, [5, 6]]

Delete Element

# ruby

aa = [0, "b", 2]

aa.delete_at(1) == "b"
p aa == [0, 2]

Push and Pop

Append/Remove from the end, and modify variable.

# ruby

xx = [0, 1, 2]

# add a element to the end
p xx.push("a") == [0, 1, 2, "a"]

# original array also changed
p xx == [0, 1, 2, "a"]

# pop removes the last element, and returns that
p xx.pop == "a"

p xx == [0, 1, 2]

Join Arrays

Array can be joined with plus sign.

# ruby

xx = ["a", "b"] + [7, 6]
p xx == ["a", "b", 7, 6]

Find the index of a element

# ruby

xx = ["a","b","c"]
p xx.index("b") == 1

# not found, return nil
p xx.index("d") == nil

Array Union, Intersection, Difference

# ruby

aa = [0, 8, 7, 3, 1]
bb = [3, 4]

# difference
p aa - bb == [0, 8, 7, 1]

# intersection
p aa & bb == [3]

# union
p aa | bb == [0, 8, 7, 3, 1, 4]

Get uniq elements

# ruby

xx = [0, 7, 3, 7]

# uniq
p xx.uniq == [0, 7, 3]

# original array not modified
p xx == [0, 7, 3, 7]

# uniq, modify original array
xx.uniq!
p xx == [0, 7, 3]

Sorting array

# ruby

xx = [0, 4, 2]

xx.sort == [0, 2, 4]

# original not changed
p xx == [0, 4, 2]

# change original
xx.sort! == [0, 2, 4]
p xx == [0, 2, 4]

Array equality test

# ruby

aa = [0, 1, 2]
bb = [0, 1, 2.0]
cc = [0, 1, "2"]

# check if 2 arrays are the same (ignores type.)
p ((aa == bb) == true)
p ((aa == cc) == false)

# check if 2 arrays are the same, and each element has the same type
p (aa.eql?(bb) == false)