0Pricing
Ruby Academy · Lesson

map, select, reject

Transform and filter.

map, select, reject is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

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

Yes — the full text of “map, select, reject” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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