Home ruby safe navigation operator (&.)
Post
Cancel

ruby safe navigation operator (&.)

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

Scenario

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:

1
cart && cart.owner && cart.owner.email

which is checking if cart is present, than check cart.owner and if it present than return cart.owner.email 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:

1
cart.try(:owner).try(:email)

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

1
cart&.owner&.email

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

This post is licensed under CC BY 4.0 by the author.