Enumerables

Map and collect

map builds a new array by running a block once per element. collect is the same method under a different name for readers who prefer it.

Objects like arrays carry these methods; a class of your own gets them by including Enumerable, which Comparable and Enumerable in your own class shows later in the course.

names = ["ada", "lin"] loud = names.map { |n| n.upcase } puts loud.inspect

Select and reject

select keeps elements where the block returns true. reject does the opposite and keeps elements where the block returns false.

Both return new arrays, so the original stays untouched for later steps.

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

Reduce into one value

reduce folds the whole collection into a single value: a total, a string, or a hash. The block receives the running result and the next element.

Pass the starting value in parentheses. Without it, Ruby uses the first element as the start, which surprises readers for empty arrays.

nums = [1, 2, 3, 4] total = nums.reduce(0) { |sum, n| sum + n } puts total

Find and sort

find returns the first element that matches the block, or nil when nothing matches. Use it instead of select when you need only one item.

sort returns a new ordered array. Add reverse for descending order, or sort_by when the order key differs from the item itself.

nums = [3, 1, 2] puts(nums.find { |n| n > 1 }) puts nums.sort.inspect

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? names = ["ada", "lin"] loud = names.map { |n| n.upcase } puts loud.inspect

    Answer

    ["ADA", "LIN"]

    map returns a new array of the block results, and inspect shows it with brackets.

  2. Which method is another name for map?

    Answer

    collect

    collect and map are the same method; the course uses map.

  3. What does this program print? nums = [1, 2, 3, 4] even = nums.select { |n| n.even? } puts even.inspect

    Answer

    [2, 4]

    select keeps the elements whose block result is true.

  4. What does reject keep?

    Answer

    The elements where the block returns false

    reject is the opposite of select.

  5. What does this program print? nums = [1, 2, 3, 4] total = nums.reduce(0) { |sum, n| sum + n } puts total

    Answer

    10

    The block adds every element to the running sum that starts at 0.

  6. What does the block of reduce receive?

    Answer

    The running result and the next element

    The first variable is the accumulator and the second the current element.

  7. What does nums.find { |n| n > 1 } return for [3, 1, 2]?

    Answer

    3

    find returns the first element that matches, and 3 comes first.

  8. What does find return when nothing matches?

    Answer

    nil

    The absence of a match is nil, which a caller can test.

  9. What does this program print? nums = [3, 1, 2] puts nums.sort.inspect

    Answer

    [1, 2, 3]

    sort returns a new array in ascending order and leaves the receiver alone.

  10. When is sort_by better than sort?

    Answer

    When the order key differs from the item itself

    sort_by takes a block that returns the key to order by.

Article author: Arthur Isaev