Getting Started With Ruby Arrays For Beginners


An introduction to Ruby arrays

Published on January 23, 2020 by Mike Chilson

Ruby Rails Programming

2 min READ

What exactly is an Array?

Arrays are objects (remember everything is an object in Ruby) that can store multiple values such as numbers or strings. Simplified, an array is a collection or list of things. Arrays are generally used to hold a collection of data that needs to be displayed or manipulated by your program. Their data can come from any source, user input, databases, or computed values. In Ruby (like many other languages), arrays are ordered, integer-indexed collections of any type object. This means that each element in an array is associated with and referred to by an index value within the array. Oh, and Ruby arrays are Zero-based so the first element is index 0, not 1.

array example

Creating an Array

There are two ways to create an array in Ruby. Literal Constructor and a Class Constructor.

array literal The literal constructor is an array that we literally create as the square brackets denote an array.

array class The class constructor also creates a new array, but by using the Ruby Array class.

It should be noted that both of these methods of creating an array produce an empty array.

Working with Ruby Arrays

There are several methods you use to do basic manipulation of an array. Let’s cover a few of them here.

Adding to an Array After your Array is created, you can add additional items to your array in the following ways:

push The push method is used to push the given element at the end of the given array and returns the array itself with the pushed elements.

push method

You can also use the shovel method « to add an element to an array.

shovel method

The string ‘Lenny’ is pushed into the end of the array by both of these methods.

unshift This method is used to insert the given element into the array but at the beginning of the array (index[0]).

unshift method

length The length method will count the elements in the array and return you that number. The length method will count all the elements in the array and return that number. length method

reverse This method will reverse the elements in the array that it is being called on. The last element in the array will now be the first, the first will be the last and so on until the whole array is reversed. reverse method

clear This method empties the array. It removes all elements.

reverse method

These are what I consider to be the “bare-bone” methods you will use with arrays in Ruby. For more methods be sure to check out the Ruby documentation at Ruby-Doc.org. I have linked to the 2.6.5 version, but other versions are available there.

Link To Array Class in Ruby Docs.

Good luck! Mike