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, cherryThis 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, completedTo 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, 5You can also use the splat operator * inside array brackets:
countdown = [*5..10]
puts countdown # Outputs: 5, 6, 7, 8, 9, 10These shortcuts are particularly useful when you need to quickly define a list of known values without the visual clutter of quotes and commas.
Challenge
EasyRead two inputs:
- A single line of space-separated words (e.g.,
red green blue yellow) - Two integers separated by a comma representing a range (e.g.,
3,7)
Perform the following tasks:
- Create an array of strings from the first input using the
%wshortcut syntax. Since the input is already a string, you'll need to usesplitto convert it to an array first, then demonstrate%wby creating a separate array with the wordsalpha,beta,gamma. - Create an array of symbols using
%iwith the valuesstart,middle,end. - 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:
- Each element of the input words array
- A blank line
- Each element of the
%warray (alpha, beta, gamma) - A blank line
- Each element of the
%isymbols array - A blank line
- 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
5If 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
13Cheat 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 arraysThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False