Blocks and Methods
Enumerable Blocks
Enumerable methods combine blocks with collection traversal.
Enumerable Blocks
enumerable_blocks.rb
scores = [2, 5, 8]
threshold =
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 =
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 =
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(",")}"
enumerable block
Methods such as `map` and `select` use block return values to build new collections.