Topic 2
Topic 2
Ruby source files are converted directly from the English source tree.
Overview
Why this topic matters ๐ก
Ruby methods can be compact while still supporting flexible calling styles. Keyword arguments communicate meaning at the call site, and blocks let callers inject behavior without creating ceremony-heavy callback interfaces.
Learning outcomes ๐ฏ
By the end of this topic, students should be able to:
Assessment focus โ
Students should be able to build a small API that remains readable for both the author and the caller.
Short Note
A good Ruby API often reads almost like a sentence. Keyword arguments help a caller understand meaning without opening the method body. Blocks let the caller supply small pieces of behavior without introducing heavy structure.
For Java developers, this is an important contrast. In Java, flexibility often invites more types, more overloads, or more scaffolding. In Ruby, the language encourages small, expressive call sites.
Ruby beauty in this topic:
Ruby caution in this topic:
Reflection prompt:
Worked Examples
Example 1: Formatting API for readable call sites ๐ก
A formatter is a realistic small API because callers often care about readability more than implementation detail.
formatter . wrap ( "warning" , left : "(" , right : ")" ) worked_examples.md ruby This is better than positional punctuation arguments because the call explains itself.
Example 2: Notification hook with a block ๐ก
Blocks are useful when the framework owns the iteration and the caller owns the side effect.
notifier . notify_all ( users ) do |message| audit_log << message end worked_examples.md ruby Why this is useful: