โ Ruby Programming learning pathRuby Programming ยท Module 6 of 10
Blocks and iterators
Blocks are chunks of code you pass to methods; they make Ruby loops elegant.
๐ Key ideas
- each runs a block for every item: marks.each { |m| puts m }.
- map transforms each item and returns a new array.
- select keeps items that match; reject removes them.
- reduce (inject) combines items into one value, like a sum.
- Use { } for one-line blocks and do ... end for multi-line ones.
๐ก Real-life example: Like giving a teacher one instruction card to apply to every student's answer sheet.
BlockCode passed to a method
mapTransform each item into a new array
selectKeep items that match
reduceCombine items into one value