A.8 Array

list of objects represented with square brackets, [].

A.8.1 Creating an Array

Array.new ⇒ Array

initializes a new empty Array.

cities = Array.new # => cities = []
or
cities = [] # => cities = []

A.8.2 .push

Array#push(Object) ⇒ Array

Adds elements to the end of an Array. Returns the modified Array.

cities.push("Chicago")
cities.push("Los Angeles")
cities.push("New York City")

or

cities = ["Chicago", "Los Angeles", "New York City"]
# Initializes and adds elements to an Array

A.8.3 .at()

Array#.at(Integer) ⇒ Object

Takes an Integer argument and return the element in that position of an Array. The following lines of code show the various forms of the .at method and return the same output.

Returns an Object

cities = ["Chicago", "Los Angeles", "New York City"]
cities.at(2)  
cities.[](2)  
cities[2]

"New York City"

Note: 1. Ruby indexes the elements in an array starting at zero, that is, the first element of an array will have the index zero. 2. Trying to access an element using an index greater than the length of the array will give you nil.
cities.at(3) # => nil 3. Using a negative index will retrieve elements from the end of the least.
cities.at(-1) # => "New York City"
cities.at(-2) # => "Los Angeles"
cities.at(-3) # => "Chicago"
cities.at(-4) # => nil

Full explanation

A.8.4 .first and .last

Array#first ⇒ Object or Array#last ⇒ Object

Retrieves and returns the first or the last element of an array.

Returns an Object

cities.first # => "Chicago"
cities.last) # => "New York City"

A.8.5 .index

Array#index(Object) ⇒ Integer

Returns an Integer that is the index of an element.
cities.index("Los Angeles") # => 1

A.8.6 .count

Array#count ⇒ Integer or Array#count(Object) ⇒ Integer

Returns the number of elements in a list, when give no arguments. If given an argument, returns the number of times that arguments occurs in the array. In both instances, this method returns an Integer

nums = [8, 3, 1, 19, 23, 3]
nums.count # => 6
nums.count(3) # => 2
nums.count(2) # => 0

Full explanation

A.8.7 .reverse (Array)

Array#reverse ⇒ Array

Returns a new ArrayArray with the elements of the original Array but in the reversed order.
nums.reverse # => [3, 23, 19, 1, 3, 8]

Full explanation

A.8.8 .sort

Array#.sort ⇒ Array

Returns a new Array with the elements of the original Array but in the sorted in increasing order.
nums.sort # => [1, 3, 3, 8, 19, 23]

A.8.8.1 Example: Sorting an Array in decreasing order

nums = [8, 3, 1, 19, 23, 3]
nums.sort # => [1, 3, 3, 8, 19, 23] 
nums.reverse # => [3, 23, 19, 1, 3, 8] 
nums.sort.reverse # => [23, 19, 8, 3, 3, 1], first sorts then reverses the Array. 

A.8.9 .shuffle

Array#shuffle ⇒ Array

Returns a new Array with the elements of the original Array but with the order shuffled randomly.
nums.shuffle # => [3, 23, 8, 19, 1, 3]
nums.shuffle # => [19, 3, 1, 8, 3, 23]

Full explanation

A.8.10 .sample

Array#sample ⇒ Array

Returns a random element from the array.
nums.sample # => 23
nums.sample # => 3

Full explanation

A.8.11 .min and .max

Array#min ⇒ Object or Array#max ⇒ Object

Retrieve the elements of minimum and the maximum values in the array.
nums.min # => 1
nums.max # => 23

Full explanation

A.8.12 .sum (Array)

Array#sum

Returns the sum of all the elements in the array.
nums.sum # => 57

Note This method only works in the elements in the Array are not a Hash

Full explanation