0Pricing
Ruby Academy · Lesson

map, select, reject

Transform and filter.

Transforming with map

map creates a new array by running the block on each element. The original is unchanged.

nums = [1, 2, 3]
doubled = nums.map { |n| n * 2 }
puts doubled.inspect
puts nums.inspect

map for Strings

map can transform any kind of value, like uppercasing words.

words = ["red", "green"]
puts words.map { |w| w.upcase }.inspect

All lessons in this course

  1. Creating Ranges
  2. The Enumerable Module
  3. map, select, reject
  4. reduce and each_with_object
← Back to Ruby Academy