Blocks and Methods
Enumerable Blocks
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
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(",")}"
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 + 16enddo |score|
pass 1 of 34boosted = scores.map do |score2|5 score + 16endAll 3 passes — pass 1 is the card above pass scorescoresboosted1 2 — — 2 5 — — 3 8 [2, 5, 8] [3, 6, 9] passing = boosted.select do |score|
8passing = boosted[3, 6, 9].select do |score|9 score >= threshold10enddo |score|
pass 1 of 38passing = boosted.select do |score3|9 score >= threshold10endAll 3 passes — pass 1 is the card above pass scoreboostedpassing1 3 — — 2 6 — — 3 9 [3, 6, 9] [6, 9] 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
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 + 16enddo |score|
pass 1 of 34boosted = scores.map do |score2|5 score + 16endAll 3 passes — pass 1 is the card above pass scorescoresboosted1 2 — — 2 5 — — 3 8 [2, 5, 8] [3, 6, 9] passing = boosted.select do |score|
8passing = boosted[3, 6, 9].select do |score|9 score >= threshold10enddo |score|
pass 1 of 38passing = boosted.select do |score3|9 score >= threshold10endAll 3 passes — pass 1 is the card above pass scoreboostedpassing1 3 — — 2 6 — — 3 9 [3, 6, 9] [6, 9] 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
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 + 16enddo |score|
pass 1 of 34boosted = scores.map do |score2|5 score + 16endAll 3 passes — pass 1 is the card above pass scorescoresboosted1 2 — — 2 5 — — 3 8 [2, 5, 8] [3, 6, 9] passing = boosted.select do |score|
8passing = boosted[3, 6, 9].select do |score|9 score >= threshold10enddo |score|
pass 1 of 38passing = boosted.select do |score3|9 score >= threshold10endAll 3 passes — pass 1 is the card above pass scoreboostedpassing1 3 — — 2 6 — — 3 9 [3, 6, 9] [] 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
scoresstarts as2,5, and8.- The first block adds
1to each score. boostedbecomes3,6, and9.thresholdis6.- The second block keeps boosted scores at least
6, sopassingis6,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=.