Ebook Studio

Topic 14

Topic 14

Ruby source files are converted directly from the English source tree.

Overview

Why this topic matters ๐Ÿ’ก

Students often see map , zip , and lambdas as clever syntax. That is the wrong framing. These are practical tools for writing code that transforms data clearly, passes behavior explicitly, and avoids unnecessary mutable setup.

This topic is about the real value of a functional style in Ruby:

Learning outcomes ๐ŸŽฏ

By the end of this topic, students should be able to:

Assessment focus โœ…

Students should be able to say what improved when the code moved from manual loops to collection transforms, and also when not to force a pipeline.

Short Note

Ruby is not asking students to become pure functional programmers. It is asking them to notice that many ordinary problems are easier to read when code is written as a series of transformations instead of a series of mutations.

Imperative style often looks like this:

rows = [] names . each_with_index do |name , index| score = scores [ index ] next if score < 60 rows << "#{name}: #{score}" end shortnote.md ruby Functional-style Ruby often looks like this:

names . zip ( scores ) . select { |_name , score| score > = 60 } . map { |name , score| "#{name}: #{score}" } shortnote.md ruby The second version is not better because it is shorter. It is better because each step answers one question:

Ruby beauty in this topic:

Ruby caution in this topic:

Reflection prompt:

Worked Examples

Example 1: Manual loop vs transformation pipeline ๐Ÿ’ก

Old style:

rows = [] quantities . each_with_index do |qty , index| price = prices [ index ] rows << qty * price end worked_examples.md ruby Functional-style Ruby:

quantities . zip ( prices ). map { |qty , price| qty * price } worked_examples.md ruby Why the second version is better here: