I have a large Mash object with lots of nested Mashes. Some of the data I want to get is several layers in like:
phone = profile.phone_numbers.all.first.phone_number
title = profile.positions.all.first.title
However, phone_numbers or positions may be nil or empty. What's the most effective way of nil checking without having to nil check each level. Is there a general technique to use?
Ick's maybe
is there to help you!
Use it like this:
phone = maybe(profile) {|p| p.phone_numbers.all.first.phone_number}
# or like this.
phone = profile.maybe.phone_numbers.
maybe.all.
maybe.first.
maybe.phone_number
Or you can prefer a simpler solution: Object#andand. It functions in a similar way.
phone = profile.andand.phone_numbers.
andand.all.
andand.first.
andand.phone_number