โ† Ruby Programming learning path
Ruby Programming ยท Module 6 of 10

Blocks and iterators

Blocks are chunks of code you pass to methods; they make Ruby loops elegant.

๐Ÿ“– Key ideas

  1. each runs a block for every item: marks.each { |m| puts m }.
  2. map transforms each item and returns a new array.
  3. select keeps items that match; reject removes them.
  4. reduce (inject) combines items into one value, like a sum.
  5. 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