Ruby - Avoid 'undefined method' Exceptions
- Intro
- Understanding the Exception
- Problematic Example
- A Fix for Our Example
- A Better Fix: Safe Navigation Operator
- Thoughts for the reader:
Intro
If you’ve been programming in Ruby for any amount of time, you’ve probably seen an error like this:
Understanding the Exception
The meaning is actually pretty straightforward if you think about it. We’re trying to call a method called “methodname” that is not defined for the object we’re calling it on. In this case, that object is nil
:
However, nil
isn’t special here, other classes do throw the same error:
We’re only really focusing on the case where an undefined method is called on nil
though. I just thought it was important to point out.
Anyways, probably the most common method you’ll see this happen with is: []
Problematic Example
You might see something like this when you’re pulling data out of a nested data structure you receive over HTTP or some other protocol:
In the above code, we assumed that every response would have a nested array as the third element of the outer array. Of course, this was not the case for response3
. There is no third element in response3
, so nil
is returned. We then try to run the []
method on that nil
and that’s when we generate the NoMethodError
:
A Fix for Our Example
Let’s fix it:
Well, that’s pretty tedious, there must be a better way. Enter the safe navigation operator!: &
A Better Fix: Safe Navigation Operator
That’s it, did you even notice? All we have to do is put a &
between our possibly-nil object, and the method being called on it. If the object is nil
, the method won’t be called, and nil
will be returned instead of an exception.
So much easier than checking every single item manually!
Thoughts for the reader:
- Is there a better way to handle this?
- Where else might you run into errors like this?