Map and Collect
Part of the Fundamentals section of Coddy's Ruby journey — lesson 68 of 88.
While each is great for performing actions on elements, sometimes you want to transform an array into a new one. The map method (also called collect) does exactly this: it creates a new array containing the results of running your block on each element.
numbers = [1, 2, 3, 4]
doubled = numbers.map { |n| n * 2 }
puts doubled # Outputs: 2, 4, 6, 8The key difference from each is that map returns a new array with the transformed values. The original array remains unchanged.
Here's a practical example that converts temperatures from Celsius to Fahrenheit:
celsius = [0, 20, 100]
fahrenheit = celsius.map { |c| c * 9 / 5 + 32 }
puts fahrenheit # Outputs: 32, 68, 212You can also use map with strings:
names = ["alice", "bob"]
capitalized = names.map { |name| name.capitalize }
puts capitalized # Outputs: Alice, BobThe methods map and collect are identical. Ruby provides both names, so you can use whichever reads better in your code. Most Ruby developers prefer map because it's shorter.
Challenge
EasyRead a single line of input containing comma-separated integers (e.g., 2,4,6,8,10).
Split the input into an array of integers, then use the map method to create a new array where each number is squared (multiplied by itself).
Print each element of the new squared array on its own line.
For example, if the input is 2,4,6,8,10, the output should be:
4
16
36
64
100If the input is 1,2,3,4,5, the output should be:
1
4
9
16
25If the input is 3,7,11, the output should be:
9
49
121If the input is 10, the output should be:
100Cheat sheet
The map method (also called collect) transforms an array into a new one by applying a block to each element. It returns a new array with the transformed values, leaving the original array unchanged.
numbers = [1, 2, 3, 4]
doubled = numbers.map { |n| n * 2 }
puts doubled # Outputs: 2, 4, 6, 8Example with temperature conversion:
celsius = [0, 20, 100]
fahrenheit = celsius.map { |c| c * 9 / 5 + 32 }
puts fahrenheit # Outputs: 32, 68, 212Example with strings:
names = ["alice", "bob"]
capitalized = names.map { |name| name.capitalize }
puts capitalized # Outputs: Alice, Bobmap and collect are identical methods. Most Ruby developers prefer map.
Try it yourself
# Read the comma-separated input
input = gets.chomp
# TODO: Write your code below
# 1. Split the input string into an array of integers
# 2. Use the map method to square each number
# 3. Print each squared number on its own lineThis 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