Intern Track Roadmap

Ruby enumerable: map

This course covers Ruby's map method, which runs a block once per element and collects each result into a new array of the same length. It compares map with each, shows that map on a hash returns an array, and explains how to get a hash back with to_h or transform_values. Lessons also cover chaining map with uniq, compact and sort, transforming in place with map!, the collect alias, and common mistakes. A final challenge builds a small score normalizer.

What you'll learn

  • ✓How map collects block results into a new array
  • ✓Choosing between map and each by what happens to the block's result
  • ✓Using map on hashes with to_h, and when to use transform_values
  • ✓Chaining map with uniq, compact, sort and other array methods
  • ✓When map! fits and why plain map is the safer default
  • ✓The collect alias and common map pitfalls
Free preview · Lesson 1 2 min read

map transforms every element

The last course set out each's job: run code once per element for its side effects, and hand back the same collection. map does the opposite job.

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...