Lesson 6 of 15

Arrays

Arrays in Ruby

An array is an ordered list of values, created with square brackets:

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", true, nil]

Accessing Elements

Arrays are zero-indexed. Negative indices count from the end:

fruits = ["apple", "banana", "cherry"]
puts fruits[0]    # apple
puts fruits[-1]   # cherry (last element)
puts fruits.first # apple
puts fruits.last  # cherry

Common Methods

numbers = [1, 2, 3]
puts numbers.length   # 3
numbers.push(4)       # add to end: [1, 2, 3, 4]
numbers.pop           # remove from end: [1, 2, 3]
puts numbers.join(", ")  # "1, 2, 3"
puts numbers.reverse.inspect  # [3, 2, 1]
puts numbers.sort.inspect     # [1, 2, 3]
puts numbers.include?(2)      # true

By convention, Ruby methods ending in ? return a boolean. You'll see this pattern throughout the standard library — include?, empty?, nil?, even?, and many more.

Your Task

Starting with numbers = [1, 2, 3, 4, 5]:

  1. Print numbers[0]
  2. Print numbers.length
  3. Push 6 to the array
  4. Print numbers.last
  5. Print numbers.join(", ")
ruby.wasm loading...
Loading...
Click "Run" to execute your code.