Ruby arrays are a fundamental data structure that is used to store data in memory. Inside a Ruby array you can store anything you want, from strings to integers and even other arrays. The elements in an array are accessed using their index, which starts at 0.
This is what an array containing the words “cat”, “dog” and “tiger” would look like:

Ruby Arrays – Basic Operations
To work with an array you will have to create one first, there are multiple ways to do it.
Initialize an empty array:
|
# Both of these do the same thing, but the second form is prefered users = Array.new users = [] |
Initialize an array with data:
|
users = ["john", "david", "peter"] |
Alternatively, you can avoid having to type the quotes for every string by doing this:
|
# Same effect as last time, but much faster to type users = %w(john david peter) |
Now that we have an array the next step would be accessing the elements that it contains.
Access array element by index:
|
users[0] # First element of the array users[1] # Second element of the array users[2] # Third element of the array |
You can also use the first and last methods:
|
users.first # First element of the array users.last # Last element of the array |
At this point you may want to add items to your array:
|
# Both of these have the same effect, << is preferred. users.push "andrew" users << "andrew" |
And finally, here is how you delete elements from your array:
|
last_user = users.pop # Removes the last element from the array and returns it user.delete_at(0) # Removes the first element of the array |
There is also the shift / unshift methods, which are similar to pop/push but take or add elements in front of the array.
|
users.unshift "robert" # Adds an element in front of the array users.shift # Removes the first element of the array and returns it |
Here is a small cheatsheet for you:
|
init -> Array.new, [], %w read -> [0], first, last add -> push, <<, unshift remove -> pop, delete_at, shift |
Iterating Over Ruby Arrays
Now that you have got an array wouldn’t it be nice if you could enumerate its contents and print them? Well the good news is that you can!
Example: Print your array using each
|
users.each { |item| puts item } |
Most of these looping operations are available thanks to the Enumerable module, which is mixed into the Array class by default.
Example: Capitalize every word in our Array using map.
|
users = users.map { |user| user.capitalize } user = users.map(&:capitalize) # This syntax is available since Ruby 1.9 |
The map method doesn’t modify the array in-place, it just returns a new array with the modified elements, so we neeed to assign the results back to a variable. There is also a map! (notice the exclamation point) method which will modify the array directly, but in general the simpler version is preferred.
Another thing you may want to do is find all the items in your array that fit certain criteria.
Example: Find all the numbers greater than 10:
|
numbers = [3, 7, 12, 2, 49] numbers.select { |n| n > 10 } # => 12, 49 |
More Array Operations
There are a lot of things you can do using arrays, like sorting them or picking random elements.
You can use the sort method to sort an array, this will work fine if all you have is strings or numbers in your array. For more advanced sorting check out sort_by.
You can also remove the duplicate elements from an array, if you find yourself doing this often you may want to consider using a Set instead.
|
numbers = [1, 3, 3, 5, 5] numbers = numbers.uniq # => [1, 3, 5] |
If you want to pick one random element from your array you can use the sample method:
You may also want to “slice” your array, taking a portion of it instead of the whole thing.
Example: Take the first 3 elements from the array, without changing it:
|
numbers.take(3) numbers[0,3] |
Operations involving multiple arrays
If you have two arrays and want to join them you can do it like this.
|
# These do the same thing # Faster, because this changes the users array users.concat(new_users) # Slower, because this creates a new array users += new_users |
You can also remove elements from one array like this, where users_to_delete
is also an array:
|
users = users - users_to_delete |
Finally, you can get the elements that appear in two arrays at the same time:
Conclusion
Arrays are very useful and they will be a powerful ally by your side. Don’t forget to check out the documentation if you are unsure of what a method does.