Arrays

Ruby is a dynamic, beginner-friendly programming language, and an Array is its workhorse collection — an ordered, index-addressable list that holds any kind of value.

Learn Arrays in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll create arrays, read items by index, add and remove elements, and use Ruby's rich array methods.

What You'll Learn in This Lesson

1️⃣ Creating, Indexing, and Modifying

Write an array with square brackets and commas. Read any item by its zero-based index , or use negative indexes to count from the end. Add with push / and remove the last with pop .

2️⃣ Slicing and Transforming

Grab a chunk with a range like arr[1..3] . Then transform with methods that return new arrays: sort , reverse , uniq , plus aggregates like min , max , and sum . Use join / split to move between arrays and strings.

Your turn. Summarise a list of test scores. Fill in the two TODO method calls, then run it.

Clean up a guest list by removing duplicates, alphabetising, and joining. Run with ruby guests.rb .

📋 Quick Reference — Arrays

Practice quiz

Given fruits = ["apple", "banana", "cherry"], what does fruits[-1] return?

  • "apple"
  • "cherry"
  • nil
  • "banana"

Answer: "cherry". Negative indexes count from the end, so [-1] is the last element, "cherry".

What does [3, 1, 4, 1, 5].uniq return?

uniq removes duplicates while preserving the order of first appearance: [3, 1, 4, 5].

Which method adds an element to the END of an array?

  • unshift
  • shift
  • push
  • pop

Answer: push. push (or <<) appends to the end; pop removes the last; shift/unshift work at the front.

What does [3, 1, 2].sort return?

sort returns a NEW ascending array [1, 2, 3] and leaves the original untouched.

Reading past the end of an array, like arr[99] on a 3-element array, returns what?

  • An IndexError
  • 0
  • nil
  • The last element

Answer: nil. Out-of-range index access returns nil rather than raising — check length first.

What does [1, 2, 3, 4, 5][1..3] return?

The range 1..3 is inclusive, grabbing indexes 1, 2, and 3: [2, 3, 4].

What does ["x", "y", "z"].join("-") produce?

join glues elements into one string using the separator: "x-y-z".

What does [1, 2, 3, 4].sum return?

  • 10
  • 24
  • 4

Answer: 10. sum adds all the elements: 1 + 2 + 3 + 4 = 10.

What is the difference between sort and sort!?

  • sort! sorts descending; sort sorts ascending
  • They are identical
  • sort returns a new array; sort! mutates the array in place
  • sort! is invalid Ruby

Answer: sort returns a new array; sort! mutates the array in place. The bang version sort! mutates the receiver; sort returns a new sorted array.

To clearly see an array's structure (brackets and quotes), you should use:

  • puts arr
  • print arr
  • arr.inspect (or p arr)
  • arr.to_s.length

Answer: arr.inspect (or p arr). inspect (and the p shortcut) show the array exactly as written in code, with brackets and quotes.