Ruby Arrays

With all of the new Ruby web frameworks, and people realizing their is more to Ruby then Rails, it is more important then ever to call yourself a Ruby hacker not a Rails programmer.

Now I know most experienced Rails developers already are great Rubyist, but for people new to Ruby through Rails I always think it is important to point out that Ruby makes Rails cool not the other way around.

One thing that I recommend to people new to Ruby is learn arrays and their relationship with iterators and blocks. So I thought a basic review Arrays would be in order, and since there is a wealth of great information on Ruby on the web, I will keep this as concise as possible.

Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C# or Java. A negative index is assumed to be relative to the end of the array-that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

Creating a new array

array =
# or
array = []

Adding array elements By position

array = []
array[0] = "first element"

To the end

array << "last element"
# This adds "last element" to the end of the array
# or
array.push("last element")
# This adds "last element" to the end of the array

To the beginning

array = ["first element", "second element"]
array.unshift("before first element")
# This adds "before first element" to the beginning of the array

Retrieving array elements

By position

array = ["first element", "second element", "third element"]
third_element = array[2]
# This returns the third element.

By positions

second_and_third_element = array[1, 2]
# This returns a new array that contains the second & third elements

Removing array elements

From the beginning

array = ["first element", "second element"]
first_element = array.shift
# This removes "first element" from the beginning of the array

From the end

last_element = array.pop
# This removes "second element" or the last element from the end of the array

Combining arrays

To a new array

array = [1, 2, 3]
array.concat([4, 5, 6, 7])
[1, 2, 3, 4, 5, 6, 7]
new_array = array + [4, 5, 6]
# Using the "+" method returns a new array, but does not modify the original array

Modifying the array

array.replace([4, 5, 6])
[4, 5, 6]

Pairing items

array = [1, 2, 3]
new_paired_array =[4, 5, 6])
[[1,4],[2, 5],[3, 6]]
# This creates an array of arrays

Flattening array of arrays

array = [1, 2, 3]
new_paired_array_flattened =[4, 5, 6]).flatten
[1, 4, 2, 5, 3, 6]

This flattens the arrays of arrays into one Array.

Collecting array elements

array = [1, 2, 3]
array.collect {|x| x * 2 }
[2, 4, 6]

Invokes block once for each element of self. Creates a new array containing the values returned by the block.

Iterating over arrays

[1, 2, 3, 4] .each {|x| puts x}

Filtering arrays

[1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}

This returns the first element that matches the block criteria.

Querying arrays

array.find_all {|item| criteria }

This returns a new array containing all the elements of the array that match the block criteria.


This returns the number of elements in the array.


This returns true if the array is empty; false otherwise.


This returns true if the array contains the item; false otherwise.

array.any? {|item| criteria }

This returns true if any item in the array matches the block criteria; false otherwise.

array.all? {|item| criteria }

This returns true if all items in the array match the block criteria; false otherwise

Hopefully if you are new to the Ruby or you have been doing some Rails work but have not really dug into Ruby, this very basic overview will spark deeper interest into the powerful (and very fun) world of Ruby.


6 thoughts on “Ruby Arrays

  1. THere was a way of shortening the array from lets say 4 elements to 3 eg:
    [a,b,c,d] => [a,b,cd]

    There was an example the the Ruby Array class I think but can’t find it anymore.

    1. Yep Jay, but Ruby is said to follow the principle of least astonishment which means that the language should behave in such a way as to minimize confusion for experienced users. When you are new to Ruby it seems like there are many ways to do the same thing, but as you become more familiar with Ruby you learn that things that seem to do the same thing have subtle differences. Most experiences Ruby programmers tend to do something only one way in Ruby.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s