← Home · Ruby skill tree

An interactive Ruby lesson

Enumerable

Enumerable provides methods for working with collections. Practice this style with arrays: choose values and turn them into new results. Build on Blocks and Arrays.

do |number| begins a block with number as its parameter; end closes it. Each method passes one element at a time to the block. These examples use numeric calculations and comparisons without changing elements in the source array.

p displays an inspected value. Predict the output, press Run, then open the expected output. Change one input and run again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.

1. Collect block results with map

map calls the block for each element and collects its result in a new array. The last expression, number * 2, gives each result. Predict both arrays.

Show expected output
[2, 4, 6]
[1, 2, 3]

Try it: Change number * 2 to number * 3. Predict both lines. These calculations leave the source array unchanged.

2. Keep matching values with select

select keeps original values when the block result is truthy. Only false and nil are falsey in Ruby. Other values, including 0, are truthy. The values stay in order; duplicates remain. Here number >= 3 tests whether a number is at least 3. Predict which values remain.

Show expected output
[3, 3]
[1, 3, 2, 3]

Try it: Change number >= 3 to number >= 2. Predict the selected values in order.

3. Compare map and select

map keeps every block result, including false and nil. select uses the result to decide whether to keep the original element. Predict both arrays for the same comparison.

Show expected output
[false, true, false, true]
[3, 3]

Try it: Change the first number >= 3 to nil. Predict which array changes. Then try 0 as the result in the select block.

4. Drop matching values with reject

reject drops elements whose block result is truthy. With the same test, select keeps those elements. Predict the remaining numbers when the test is number < 0.

Show expected output
[3, 0, 2]
[3, -1, 0, 2]

Try it: Change number < 0 to number <= 0. Predict whether zero remains.

5. Filter before transforming

selected stores the values at least 4. Then map multiplies those values by 10. The test acts on the source values before multiplication. Predict both lines.

Show expected output
[50, 40]
[2, 5, 1, 4]

Try it: Change the threshold from 4 to 5. Predict the result. Explain why the original 2 is excluded before multiplication.

6. Start with an empty array

An empty array has no elements to pass to either block. Predict the three arrays.

Show expected output
[]
[]
[]

Try it: Change [] to [3]. Predict each line. Then try [3, 1, 3] and explain the repeated values.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Create prices = [5, 12, 8, 20].
  2. Use select to store prices at least 10 in eligible.
  3. Use map on eligible to subtract 2 from each price. Store the result in discounted.
  4. Display discounted, then prices. Explain why the source is unchanged and which values the comparison tests.
Show one solution and its output
prices = [5, 12, 8, 20]
eligible = prices.select do |price|
  price >= 10
end
discounted = eligible.map do |price|
  price - 2
end
p(discounted)
p(prices)

Output:

[10, 18]
[5, 12, 8, 20]

Try [5, 8], then []. Predict both lines when no prices match. The comparison tests the original prices before the subtraction.

Ruby documentation

See the Ruby 4.0 documentation for Enumerable and the array methods map, select, and reject. The syntax guide explains blocks and truthy and falsey values.

Return to the Ruby skill tree to choose another topic.