Menu
Coddy logo textTech

Array Shortcuts

Part of the Fundamentals section of Coddy's Ruby journey — lesson 64 of 88.

Ruby provides convenient shortcuts for creating arrays, especially when working with strings or sequential numbers. These shortcuts save time and make your code cleaner.

The %w syntax creates an array of strings without needing quotes or commas:

fruits = %w[apple banana cherry]
puts fruits  # Outputs: apple, banana, cherry

This is equivalent to writing ["apple", "banana", "cherry"] but much more concise. Each word separated by a space becomes a separate element.

For arrays of symbols, use %i:

statuses = %i[pending active completed]
puts statuses  # Outputs: pending, active, completed

To create arrays of sequential numbers, you can convert a range to an array using to_a:

numbers = (1..5).to_a
puts numbers  # Outputs: 1, 2, 3, 4, 5

You can also use the splat operator * inside array brackets:

countdown = [*5..10]
puts countdown  # Outputs: 5, 6, 7, 8, 9, 10

These shortcuts are particularly useful when you need to quickly define a list of known values without the visual clutter of quotes and commas.

challenge icon

Challenge

Easy

Read two inputs:

  1. A single line of space-separated words (e.g., red green blue yellow)
  2. Two integers separated by a comma representing a range (e.g., 3,7)

Perform the following tasks:

  1. Create an array of strings from the first input using the %w shortcut syntax. Since the input is already a string, you'll need to use split to convert it to an array first, then demonstrate %w by creating a separate array with the words alpha, beta, gamma.
  2. Create an array of symbols using %i with the values start, middle, end.
  3. Create an array of sequential numbers from the range provided in the second input using the splat operator * inside array brackets.

Print the following on separate lines:

  1. Each element of the input words array
  2. A blank line
  3. Each element of the %w array (alpha, beta, gamma)
  4. A blank line
  5. Each element of the %i symbols array
  6. A blank line
  7. Each element of the number range array

For example, if the inputs are red green blue and 1,5, the output should be:

red
green
blue

alpha
beta
gamma

start
middle
end

1
2
3
4
5

If the inputs are sun moon star and 10,13, the output should be:

sun
moon
star

alpha
beta
gamma

start
middle
end

10
11
12
13

Cheat sheet

Ruby provides shortcuts for creating arrays that make code cleaner and more concise.

The %w syntax creates an array of strings without quotes or commas:

fruits = %w[apple banana cherry]
# Equivalent to ["apple", "banana", "cherry"]

The %i syntax creates an array of symbols:

statuses = %i[pending active completed]
# Equivalent to [:pending, :active, :completed]

Convert a range to an array using to_a:

numbers = (1..5).to_a
# Creates [1, 2, 3, 4, 5]

Use the splat operator * inside array brackets to create arrays from ranges:

countdown = [*5..10]
# Creates [5, 6, 7, 8, 9, 10]

Try it yourself

# Read the space-separated words
words_input = gets.chomp

# Read the range (two integers separated by comma)
range_input = gets.chomp

# TODO: Write your code below
# 1. Create an array from words_input using split
# 2. Create a %w array with alpha, beta, gamma
# 3. Create a %i array with start, middle, end
# 4. Parse the range and create an array using splat operator (*)

# Output each array's elements on separate lines with blank lines between arrays
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals