Ruby Academy · Lesson

map, select, reject

Transform and filter.

Lesson 3 of 413 steps

map, select, reject is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

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

The Symbol Shortcut

When the block just calls one method, use &:method as a shortcut.

words = ["a", "b", "c"]
puts words.map(&:upcase).inspect

map with Index

Chain with_index when you also need positions.

letters = %w[a b c]
result = letters.map.with_index { |l, i| "#{i}:#{l}" }
puts result.inspect

Filtering with select

select (alias filter) keeps only elements where the block is true.

nums = [1, 2, 3, 4, 5, 6]
evens = nums.select { |n| n.even? }
puts evens.inspect

Filtering Strings

select works on any condition, such as keeping long words.

words = ["hi", "hello", "hey", "welcome"]
puts words.select { |w| w.length > 3 }.inspect

Removing with reject

reject is the mirror of select. It drops elements where the block is true.

nums = [1, 2, 3, 4]
puts nums.reject { |n| n.even? }.inspect

Chaining map and select

Because each returns a new array, you can chain methods into a pipeline.

nums = [1, 2, 3, 4, 5]
result = nums.select { |n| n.odd? }.map { |n| n * 10 }
puts result.inspect

filter_map

filter_map combines filtering and mapping in one pass: it keeps non-false block results.

nums = [1, 2, 3, 4]
result = nums.filter_map { |n| n * 2 if n.even? }
puts result.inspect

flat_map

flat_map maps and then flattens one level of nested arrays.

pairs = [[1, 2], [3, 4]]
puts pairs.flat_map { |p| p }.inspect

partition

partition splits a collection into two arrays: matches and non-matches.

nums = [1, 2, 3, 4, 5]
evens, odds = nums.partition { |n| n.even? }
puts evens.inspect
puts odds.inspect

Quick Check

Check transform and filter.

Recap

You mastered transformation and filtering:

  • map to transform, with &:method shortcut
  • select/filter and reject
  • chaining, filter_map, flat_map, partition

Next: reduce and each_with_object for aggregation.

puts [1, 2, 3, 4].select(&:even?).map { |n| n * n }.inspect
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “map, select, reject” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “map, select, reject”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “map, select, reject”?

Transform and filter. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “map, select, reject” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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