Most Handy Ruby Array Methods
Shaher Shamroukh
Posted on September 6, 2021
The Handiest Ruby Array Methods
Array is a fundamental class in ruby, so whether you are building a ruby program or developing a rails app, having a good knowledge of array methods will be very helpful.
and it's a most to know some of these methods.
If you want to know more about Ruby arrays, take a look at Ruby-Docs
Quick tipπ
You can have access to ruby docs via the terminal by running the following command rvm docs generate
then you can look up anything for example we will see the .first
array method so we run ri Array#first
also by running ri Array
this will output all the information about the array class and it's instance methods as well it's class methods.
So without further ado let's create our array and get started.
arr = Array(1..5)
that's same as arr = [1, 2, 3, 4, 5]
.first
As the name implies this method gets the first element of the array.
arr.first
=> 1
.last
As the name implies this method gets the last element of the array.
arr.last
=> 5
.length
This the most common one, this method will return the number of elements in the array
arr.length
=> 5
.take
The method returns an array with the number of elements we pass to it as an argument.
arr.take(4)
=> [1, 2, 3, 4]
.drop
The method returns an array without the number of elements we pass to it as an argument.
arr.drop(4)
=> [5]
.shift
This will remove the first element of an array.
arr.shift
=> 1
arr
=> [2, 3, 4, 5]
.pop
This will remove the last element of an array.
arr.pop
=> 5
arr
=> [2, 3, 4]
.unshift
This will add the element to the beginning of the array.
arr.unshift(1)
=> [1, 2, 3, 4]
.push
This will add the element to the end of the array.
arr.push(5)
=> [1, 2, 3, 4, 5]
.insert
With this method we can add elements to the array at any position.
arr = [1, 2, 3, 4, 5]
arr.insert(3, nil, true, "Mango")
=> [1, 2, 3, nil, true, "Mango", 4, 5]
.compact
This method will remove the nil values from the array
arr = [nil, 1, 2, nil, 3, 4, 5, nil]
arr.compact
=> [1, 2, 3, 4, 5]
array index
When we want to access a specific element in the array we use it's index since each element in the array has an index which starts at 0, and if the index does not exist in the array we get nil returned.
arr[0]
=> 1
arr[3]
=> 4
arr[-1]
=> 5
arr[2] = 7
arr
=> [1, 2, 7, 4, 5]
Another way to access a particular array element is by using the at method
arr.at(0)
=> 1
.delete_at
This method will remove the element based on the index we provide as an argument.
arr.delete_at(0)
=> [2, 3, 4, 5]
.reverse
This method will reverse the array items, keep in mind it will not mutate the array.
arr.reverse
=> [5, 4, 3, 2]
arr
=> [2, 3, 4, 5]
.include?
This method checks if the given argument included in the array or not.
arr.include?(5)
=> true
arr.include?(9)
=> false
.empty?
This method checks whether an array contains any elements at all.
arr = [1, 2, 3, 4, 5]
arr.empty?
=> false
arr = []
arr.empty?
=> true
.concat
The concat method takes multiple arrays as an argument and returns one array.
arr = [0, 1, 2, 3]
arr.concat([4, 5, 6, 7], Array(8..10))
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.uniq
This method returns the unique elements in a given array and remove the duplicate ones
arr = [1, 2, 2, 2, 3, 4, 4, 5]
arr.uniq
=> [1, 2, 3, 4, 5]
Now on top of those methods the Array class include the Enumerable module, as stated in the ruby API The Enumerable mixin provides collection classes with several traversal and searching methods.
Please checkout the Ruby-Guide.
I hope you enjoyed the reading as i have enjoyed writing itπππ
Posted on September 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.