Junior Track Roadmap

Ruby idioms: safe navigation

This course explains how Ruby's safe navigation operator &. works. It compares &. chains with && chains and shows that &. evaluates each link once, returning nil for the whole chain when any link is nil. It covers arguments, blocks and operator methods, and what plain . does on nil. It pairs &. with || for default values. It explains when &. hides a bug instead of preventing one. It closes by reviewing real lines from the app's own code.

What you'll learn

  • ✓How &. chains differ from && chains in evaluation and short-circuiting
  • ✓Using &. with arguments, blocks and operator methods such as >
  • ✓Why plain . raises NoMethodError on nil and what check &. adds
  • ✓Combining &. with || for defaults, and the false value trap
  • ✓Telling legitimately optional values apart from nil values that signal a bug
  • ✓Reading real uses of &. in an application's code
Free preview · Lesson 1 3 min read

Recap: the one line you've already seen

The lesson "The AND operator", in Logic: boolean algebra, taught && and its short-circuit behavior with this example:

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...