Arrays

All about arrays in Ruby


push() (add at end)

Adds the given object(s) on to the end of this array.

a = [1, 2, 3]
a.push(4)
a    #=> [1,2,3,4]
 
a.push(5)
a    #=> [1, 2, 3, 4, 5]

pop() (remove at end)

Removes the last element from self and returns it, or nil if the array is empty.

a = [ "a", "b", "c", "d" ]
a.pop     #=> "d"
a.pop(2)  #=> ["b", "c"]
a         #=> ["a"]

unshift() (add at front)

Adds a new item to the beginning of an array.

arr = [1;2;3]
arr.unshift(0) 
arr   #=> [0, 1, 2, 3]

shift() (remove at front)

Removes the first element from self and returns it, or nil if the array is empty

a = [ "a", "b", "c", "d" ]
a.shift     #=> "a"
a.pop(2)  #=> ["b", "c"]
a         #=> ["d"]