29
.
04
.
2024
20
.
09
.
2022
Ruby on Rails
Backend
Ruby
Tutorial

Safe navigation operator '&.' vs '.try' in Rails

Mateusz Woźniczka
Ruby Developer

Safe navigation operator '&.' vs '.try' in Rails

We are back to our Blog app, and this time we want to get the name of the post's author. The task is really straightforward, and we end up with the following piece of code:

post.author.name

We will get our desired result, only if post.author and post.author.name are defined - otherwise, we will end up with undefined method error. If we want to avoid those errors, we can make use of some ifs:

if post
  if post.author
    if post.author.name
      post.author.name
    end
  end
end

This solution gets the job done, but looks extremely bad. Fortunately, Ruby provides a safe navigation operator &. and Rails (or ActiveSupport to be more specific) provides the try method, that can help avoid undefined method exceptions without extensive use of if statements.

In this post, we will compare both of those methods, and look for the differences between them.

Examples

Happy path

First, let's see how they behave when everything is defined.

author = Author.new(name: 'Joe')
post = Post.new(author: author)

post.author.name
> "Joe"

post&.author&.name
> "Joe"

post.try(:author).try(:name)
> "Joe"

There are no big surprises. When there is no nil along the chain both &. and try return the author's name.

Post without an author

post = Post.new

post.author.name
> NoMethodError (undefined method 'name' for nil:NilClass)

post&.author&.name
> nil

post.try(:author).try(:name)
> nil

As expected, both methods return nil instead of calling the name method on an empty author.

Nil post

Let's see how both methods behave when they are used to call chained undefined methods for nil

nil.author.name
> NoMethodError (undefined method 'author' for nil:NilClass)

nil&.author&.name
> nil

nil.try(:author).try(:name)
> nil

Just like in the previous example both methods returned nil instead of calling the author method nil.

Calling post's undefined method

Now let's get back to post, and see what happens when we try to call an undefined method

post = Post.new

post.undefined_method
> NoMethodError (undefined method 'undefined_method' for <Post:0x00007fe4ee8f5e50>)

post&.undefined_method
> NoMethodError (undefined method 'undefined_method' for <Post:0x00007fe4ee8f5e50>)

post.try(:undefined_method)
> nil

Here is the first difference between the discussed functions. Safe navigation operator returns nil only if the receiver is nil as well - it does not do any validation of called methods. On the other hand, try is returning nil whenever the receiver does not respond to the call.

Calling nil's defined method

Let's go back to the example with nil as the receiver of the first call, but this time we will extend NilClass, so the called method is actually defined.

class NilClass
  def name
    'called nil name'
  end
end

nil.name
> "called nil name"

Now, nil respond to the name method. Let’s see how both methods are handling that:

nil&.name
> nil

nil.try(:name)
> nil

The safe navigation operator behaves as expected - it does not care about the method implementation - it checks only if the receiver is nil. Method try is behaving in the same matter - if the receiver is nil it is not checking if it responds to called method, instead, it returns nil as well.

Wrap up

Safe navigation operator &. is provided by Ruby. It returns nil only if the receiver of the method is nil as well. It does not check if called method is defined.

Rails provides us with the try method, which checks if the receiver (which is not nil) responds to the called method, and if not it returns nil instead of throwing an exception.

Mateusz Woźniczka
Ruby Developer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

WebUSB - Bridge between USB devices and web browsers

14
.
11
.
2023
Burak Aybar
Ruby on Rails
Frontend
Backend
Tutorial

Visuality comes to town - this time it's Poznań

14
.
11
.
2023
Michał Piórkowski
Visuality
HR

CSS Modules in Rails

14
.
11
.
2023
Adam Król
Ruby on Rails
Tutorial
Backend
Frontend

How to choose a software house.

14
.
11
.
2023
Michał Piórkowski
Ruby on Rails
Business
Visuality

JSON API versus the NIH syndrome

14
.
11
.
2023
Nadia Miętkiewicz
Backend
Frontend
Tutorial

From Idea to Concept

02
.
10
.
2024
Michał Krochecki
Ruby on Rails
Business
Startups

Styling React Components

14
.
11
.
2023
Umit Naimian
Ruby on Rails
Frontend
Tutorial

How good design can help your business grow

14
.
11
.
2023
Lukasz Jackiewicz
Design
Business
Marketing

TODO not. Do, or do not.

29
.
11
.
2023
Stanisław Zawadzki
Ruby on Rails
Software

CS Lessons #003: Density map in three ways

14
.
11
.
2023
Michał Młoźniak
Ruby
Backend
Tutorial
Software

Clean code for the win

14
.
11
.
2023
Michał Piórkowski
Ruby on Rails
Backend
Frontend
Business

Crowd-operated Christmas Lights

14
.
11
.
2023
Nadia Miętkiewicz
Ruby on Rails
Backend

How to startup and be mature about it

14
.
11
.
2023
Rafał Maliszewski
Ruby on Rails
Startups
Business

A journey of a thousand miles begins with workshops

14
.
11
.
2023
Michał Piórkowski
Software
HR

CS Lessons #002: Data structures

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

Summary of Phoenix workshop at Visuality

14
.
11
.
2023
Karol Słuszniak
Ruby on Rails
Visuality
Backend

CS Lessons #000: Introduction and motivation

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

CS Lessons #001: Working with binary files

14
.
11
.
2023
Michał Młoźniak
Ruby
Software

Working with 40-minute intervals

14
.
11
.
2023
Sakir Temel
Software
HR

THE MATURE TECH STARTUP DILEMMA: WHAT'S NEXT

14
.
11
.
2023
Susanna Romantsova
Startups

Win MVP workshop!

14
.
11
.
2023
Susanna Romantsova
Startups

FINTECH WEEK IN OSLO: WHATs & WHYs

14
.
11
.
2023
Susanna Romantsova
Conferences

MY FIRST MONTH AT VISUALITY

14
.
11
.
2023
Susanna Romantsova
Visuality
HR

NASA 1st global hackaton in Poland? Visuality Created it!

14
.
11
.
2023
Rafał Maliszewski
Ruby on Rails

Berlin StartupCamp 2016 summary

14
.
11
.
2023
Michał Piórkowski
Conferences
Startups