Enumerable methods combine blocks with collection traversal.

enumerable block Methods such as `map` and `select` use block return values to build new collections.

Enumerable Blocks

threshold
enumerable_blocks.rb
Replay: real traced execution (multi-file project)
scores = [2, 5, 8]
threshold = 6

boosted = scores.map do |score|
  score + 1
end

passing = boosted.select do |score|
  score >= threshold
end

puts "boosted=#{boosted.join(",")}"
puts "threshold=#{threshold}"
puts "passing=#{passing.join(",")}"
scores = [2, 5, 8]
threshold = 4

boosted = scores.map do |score|
  score + 1
end

passing = boosted.select do |score|
  score >= threshold
end

puts "boosted=#{boosted.join(",")}"
puts "threshold=#{threshold}"
puts "passing=#{passing.join(",")}"
scores = [2, 5, 8]
threshold = 10

boosted = scores.map do |score|
  score + 1
end

passing = boosted.select do |score|
  score >= threshold
end

puts "boosted=#{boosted.join(",")}"
puts "threshold=#{threshold}"
puts "passing=#{passing.join(",")}"
  1. scores ← [2, 5, 8], threshold ← 6

    1scores→ [2, 5, 8] = [2, 5, 8]2threshold→ 6 = 6  #@threshold=4, 1034boosted = scores[2, 5, 8].map do |score|5  score + 16end
  2. do |score|

    pass 1 of 3
    4boosted = scores.map do |score2|5  score + 16end
    All 3 passes — pass 1 is the card above
    passscorescoresboosted
    12
    25
    38[2, 5, 8][3, 6, 9]
  3. passing = boosted.select do |score|

    8passing = boosted[3, 6, 9].select do |score|9  score >= threshold10end
  4. do |score|

    pass 1 of 3
    8passing = boosted.select do |score3|9  score >= threshold10end
    All 3 passes — pass 1 is the card above
    passscoreboostedpassing
    13
    26
    39[3, 6, 9][6, 9]
  5. puts "boosted=#{boosted.join(",")}"

    12puts "boosted=#{boosted.join(",")3,6,9}"13puts "threshold=#{threshold6}"14puts "passing=#{passing.join(",")6,9}"
    outputboosted=3,6,9
    threshold=6
    passing=6,9
  1. scores ← [2, 5, 8], threshold ← 4

    1scores→ [2, 5, 8] = [2, 5, 8]2threshold→ 4 = 434boosted = scores[2, 5, 8].map do |score|5  score + 16end
  2. do |score|

    pass 1 of 3
    4boosted = scores.map do |score2|5  score + 16end
    All 3 passes — pass 1 is the card above
    passscorescoresboosted
    12
    25
    38[2, 5, 8][3, 6, 9]
  3. passing = boosted.select do |score|

    8passing = boosted[3, 6, 9].select do |score|9  score >= threshold10end
  4. do |score|

    pass 1 of 3
    8passing = boosted.select do |score3|9  score >= threshold10end
    All 3 passes — pass 1 is the card above
    passscoreboostedpassing
    13
    26
    39[3, 6, 9][6, 9]
  5. puts "boosted=#{boosted.join(",")}"

    12puts "boosted=#{boosted.join(",")3,6,9}"13puts "threshold=#{threshold4}"14puts "passing=#{passing.join(",")6,9}"
    outputboosted=3,6,9
    threshold=4
    passing=6,9
  1. scores ← [2, 5, 8], threshold ← 10

    1scores→ [2, 5, 8] = [2, 5, 8]2threshold→ 10 = 1034boosted = scores[2, 5, 8].map do |score|5  score + 16end
  2. do |score|

    pass 1 of 3
    4boosted = scores.map do |score2|5  score + 16end
    All 3 passes — pass 1 is the card above
    passscorescoresboosted
    12
    25
    38[2, 5, 8][3, 6, 9]
  3. passing = boosted.select do |score|

    8passing = boosted[3, 6, 9].select do |score|9  score >= threshold10end
  4. do |score|

    pass 1 of 3
    8passing = boosted.select do |score3|9  score >= threshold10end
    All 3 passes — pass 1 is the card above
    passscoreboostedpassing
    13
    26
    39[3, 6, 9][]
  5. puts "boosted=#{boosted.join(",")}"

    12puts "boosted=#{boosted.join(",")3,6,9}"13puts "threshold=#{threshold10}"14puts "passing=#{passing.join(",")(empty)}"
    outputboosted=3,6,9
    threshold=10
    passing=

Follow the Enumerable

  1. scores starts as 2, 5, and 8.
  2. The first block adds 1 to each score.
  3. boosted becomes 3, 6, and 9.
  4. threshold is 6.
  5. The second block keeps boosted scores at least 6, so passing is 6,9. | original | boosted | kept with threshold 6? | | --- | --- | --- | | 2 | 3 | no | | 5 | 6 | yes | | 8 | 9 | yes |

Exercise: enumerable_blocks.rb

Reproduce boosted=3,6,9 and passing=6,9, then use the pinned threshold variants to predict passing=6,9 and passing=.