Ruby Arrays Cheat Guide for Beginners

Published: 2014-07-29
You will encounter Arrays early on in Ruby as they are a great way to store a list of items.
Here are the methods I use regularly when dealing with Arrays in Ruby and Rails.

Note: Feel free to copy the examples into a Ruby console to see them in action.

Creating Arrays

There are multiple ways to create an array. Here are my favorites.

Create an empty array

test_array = [] # []

Create an array of values

test_array = ['one', 'second', 3, 'last'] # ["one","second", 3, "last"]
Note: Notice how the integer 3 stays as an integer

Create an array with values separated by whitespace, this is handy so you don't need all the quotes

test_array = %w(start two 3 four last_entry) # ["start","two","3","four","last_entry"] 
Note: the integer 3 is converted to a string.

Get details about the Array

For the following examples, we are going to use the following test_array

test_array = %w(start two 3 four last_entry)

How long is the Array?

test_array.length # 5

First item in the array, can also specify the index (which starts with 0)

test_array.first # "start"
test_array[0] # "start"

Last item in the array

test_array.last # "last_entry"

Second item in the array (remember index starts with 0)

test_array[1] # "two"

Third item in the array

test_array[2] # "3"

Show each item in the Array

test_array.each do |item|
  puts item
end
# start
# two
# 3
# four
# last_entry

Show each item in the Array with index

test_array.each_with_index do |item, index|
  puts "This '#{item}' has an index of: #{index}, it is item number #{index + 1}"
end
# This 'start' has an index of: 0, it is item number 1
# This 'two' has an index of: 1, it is item number 2
# This '3' has an index of: 2, it is item number 3
# This 'four' has an index of: 3, it is item number 4
# This 'last_entry' has an index of: 4, it is item number 5

Convert an Array to a string with your own delimiter

test_array.join(',') # "start,two,3,four,last_entry"
test_array.join(';') # "start;two;3;four;last_entry"
Note: The join string can be any thing. Use " for special characters such as \n

Searching the Array

For the following examples, we are going to use the following test_array:

test_array = %w(start two 3 four last_entry)

Search the array to see if an item exists within the array, returns true or false

test_array.include? 'start' # true
test_array.include? 'end' # false
test_array.include? 'star' # false
Note: This only works on the full entry, it can't do partial searches.

Search the array with a regular expression, returns an array of matching items

test_array.grep(/o/) # ["two","four"]
test_array.grep(/^s/) # ["start"]
Note: Great for partial searches.

Search the array using a block, returns an array of matching items

test_array.select {|a| a.include? 'st'} # ["start","last_entry"]
Note: The include? partial search works as it is done on the string and not the whole array

Modify the Array

For the following examples, we are going to use the following test_array, you will need to recreate it before each example.

test_array = %w(start two 3 four last_entry)

Add an item to the end of an array

test_array << 'new item' # ["start","two","3","four","last_entry","new item"]

Get the first item from the array and delete it from the array

test_array.shift # "start"

Get the last item from the array and remove it from the array

test_array.pop # "last_entry"

Modify each item in the array using a block, supports !

test_array.map {|a| a.upcase} # ["START", "TWO", "3", "FOUR", "LAST_ENTRY"]
Remember that a method with ! means that the variable will be modified directly.

Rails specific Array methods

Rails allows you to use second, third, fourth and fifth

test_array.second # "two"
test_array.third # "3"
test_array.fourth # "four"
test_array.fifth # "last_entry"

Resources

Ruby Array Class - The complete list of all Array methods.

Rails Array Class - The extra Array methods you get when using Rails



comments powered by Disqus