Arrays are important to us programmers. They are considered one of the ‘primitive’ data types in every programming language. Let’s discuss what an array is first. More specifically, how an array works in Ruby.
Each item or element in an array is associated with and referred to by its index. The index of the first element of any array is zero. Each of these elements take up a specific amount of space in memory and are contiguous in memory.
Let’s take this array for example. If the first element in this array takes up 8 bits of memory. The next element will also take up exactly the next block of 8 bits in memory. This is why arrays are considered contiguous in memory.
Ruby tries to make a programmers life simple by including many convenient built-in array methods right out of the box. As a reference to my future self and hopefully a guide for anyone looking for array methods…here we go.
Array.new(3, {})
#=> [{}, {}, {}]
Array.new(5, true)
#=> [true, true, true, true, true]
We usually use the literal syntax to create an empty array. You can use the ‘.new’ method on the Array class and pass it the number of elements as the first argument and the default object in the second element.
arr = [ 4 , 2 , 1 , 10 , 20 ]
arr[0]
#=> 4
arr[-1]
#=> 20
arr[2..4]
#=> [1, 10, 20]
arr.first
#=> 4
arr.last
#=> 20
Square bracket notation is used to access the element at the passed index. When passed in a negative index, it will retrieve elements starting from the end of the array. So arr[-1] gives you the last element of the array. You can also use spread notation to retrieve a set of elements. arr[2..4] gives you all elements starting at index 2 to index 4. The ‘.first’ and ‘.last’ methods are special methods that return the first or last element.
arr = [ 4 , 2 , 1 , 10 , 20 ]
arr.take(3)
#=> [4, 2, 1]
arr.drop(2)
#=> [1, 10, 20]
The ‘.take’ method will only return first ’n’ number of elements while the ‘.drop’ method will only return the the elements after ’n’ elements have been dropped. Both methods return a new array and do not mutate the original.
arr = [ 4 , 2 , 1 , 10 , 20 ]
arr.count
#=> 5
arr.length
#=> 5
Both ‘.count’ and ‘.length’ query the array for their size. Pretty standard stuff.
arr = []
arr.empty?
#=> true
This is a handy method to check to see if an array is empty.
arr = ["hello", "goodbye", "alorra"]
arr.includes?("alorra")
#=> true
In my opinion this is a very useful method to have in the toolbox. It queries the array and checks to see if the argument passed in exists as an element in the array.
arr = [1, 2, 3, 4, 5]
arr.push(6)
#=> [1, 2, 3, 4, 5, 6]
arr.unshift(2)
#=> [2, 1, 2, 3, 4, 5, 6]
arr.insert(4, "?")
#=> [2, 1, 2, 3, "?", 4, 5, 6]
arr.pop
#=> 6
arr
#=> [2, 1, 2, 3, "?", 4, 5]
arr.shift
#=> 2
arr
#=> [1, 2, 3, "?", 4, 5]
arr = [1, 1, 2, 2, 3, 3]
arr.uniq
#=> [1, 2, 3]
arr
#=> [1, 1, 2, 2, 3, 3]
As you can see, .uniq allows us to get rid of any repeating values from the array without mutating the original array.
arr = [1, 2, 3, 4, 5]
arr.each {|n| puts n }
1
2
3
4
5
#=> [1, 2, 3, 4, 5]
.each is one of the standard iterators that is very commonly used because it is flexible in its usage. The purpose is to go through a list or array one by one and take some sort of action whether it be to print out each element or sort an element or create a new array entirely, you can use this method.
arr = [1, 2, 3, 4, 5]
arr.map {|n| n * 3 }
#=> [3, 6, 9, 12, 15]
arr
#=> [1, 2, 3, 4, 5]
.map allows us to iterate through an array and return a brand new array. In this example, I am taking each element ’n’ and multiplying it by 3. The return value is a brand new array with each element multiplied by 3. Note that the original array is unchanged.
arr = [1, 2, 3, 4, 5]
arr.select {|n| n > 2 }
#=> [3, 4, 6]
In this example of .select, I am specifying that I want a brand new array where the elements are all greater than 2. This method is a good way to filter an array by customizing the criteria to suit your needs.
arr = [1, 2, 3, 4, 5]
arr.reject {|n| n < 5 }
#=> [5]
Similar to select in that it returns elements based on the block of code. However it essentially ‘rejects’ any element that fits the criteria of ‘n < 5’
arr = [2, 1, 5, 3, 4]
arr.sort
#=> [1, 2, 3, 4, 5]
Not only does the standard Ruby library provide us with a .sort method that is self explanatory, but it gives us a very fast sorting algorithm under the hood. The native .sort method utilizes quicksort. With an O(log n) runtime, embrace it!
That’s all for now…but I will continue to add more methods to this blog!