Module 04 Β· Idioms, Errors & Gems β Lesson 2 of 6 Β· ~12 min
Safe Navigation & Duck Typing
&. β safe navigation
You've written booking&.walker&.name conceptually already if you've used Swift's ?. or JS's ?. β Ruby's &. (the "safe navigation operator") is the same idea: call the method only if the thing on the left isn't nil; if it IS nil, the whole expression short-circuits to nil instead of raising.
booking&.walker&.name
Read that chain left to right: if booking is nil, stop, result is nil. Otherwise call .walker β if THAT'S nil, stop, result is nil. Otherwise call .name. One typo-proof line instead of three nested nil-checks.