Challenge 2: Safe Navigation on a Chain — Solution class Profile attr_accessor :avatar_url def initialize(avatar_url = nil) @avatar_url = avatar_url end end class User attr_accessor :profile def initialize(profile = nil) @profile = profile end end def display_avatar(user) user&.profile&.avatar_url || "default-avatar.png" end user_with_avatar = User.new(Profile.new("photo.jpg")) user_without_profile = User.new puts display_avatar(user_with_avatar) puts display_avatar(user_without_profile) =begin Output: photo.jpg default-avatar.png Notes: - user_with_avatar has a full chain: a User with a Profile that has an avatar_url, so user&.profile&.avatar_url resolves all the way through to "photo.jpg". - user_without_profile has no profile at all (profile defaults to nil in User#initialize). user&.profile succeeds (user is not nil), but the result (nil) means &.avatar_url short-circuits immediately instead of raising NoMethodError on nil. - || "default-avatar.png" supplies the fallback any time the whole &.-chain resolves to nil, whether because profile itself is missing or because avatar_url was never set on an existing profile. - Without &., user_without_profile.profile.avatar_url would raise NoMethodError the moment .avatar_url was called on nil. =end