ruby safe navigation operator (&.)

This post you will learn about the most interesting addition to Ruby 2.3.0, the Safe Navigation Operator(&.).


Lets say we have a cart object, which belongs to an owner and we want to know the email of the owner.

Most common solution we find Devs doing in this situation is:

cart && cart.owner &&

which is checking if cart is present, than check cart.owner and if it present than return as a result, which is tidious to write and doesn't looks good, also imagine, if there are multi level associations to work on.

Now another solution is:


bit shorter and cleaner than above solution but we can improve this by using &. operator:


It will return the email address of owner, if cart and owner present, otherwise, return nil.