The first preview of Ruby 2.3.0 was recently released. With it comes the announcement that one of the new features being integrated in this new version is the safe navigation operator.
Sometimes, you need to check if an object is
nil before calling a method on it. You've probably written code that looks a lot like this at some point:
user = User.find_by email: params[:email] name = user.name if user
If you're using Ruby on Rails, and more specifically, Active Support Core Extensions. You're very likely to be using the try method. This method behaves a lot like
Object#send except that it always returns
nil if you call it on
nil, independently of what method you ask it to execute. So, using
try, you could safely rewrite the code above as following.
user = User.find_by email: params[:email] name = user.try(:name)
This can be very handy when you're not sure if the object you got back from somewhere else won't be
nil. And it's a lot less code to write than an
Give the popularity of Ruby on Rails, a lot of Ruby developers are already using
try for safe navigation purposes. So, the Ruby core team decided to include this functionality in the language itself. Once Ruby 2.3.0 comes out, you'll be able to write the code above using the safe navigation operator,
user = User.find_by email: params[:email] name = user&.name
It looks cleaner and, best of all, it doesn't depend on any external library. It's embedded in the language itself.
Then you can open open irb and see it working for yourself.
2.3.0 :001 > a = Object.new => #<Object:0x007f85c2a00448> 2.3.0 :002 > a&.class => Object 2.3.0 :003 > a = nil => nil 2.3.0 :004 > a&.class => nil
Now, you may be asking yourself why was the the symbol
&. chosen for this operator. Specially if you look at how other languages have implemented this feature.
This piece of code would be valid for Swift, Groovy, C#, Java, and maybe others that I don't know about.
Ruby chose to use the
&. symbol instead of
?. because the character
? can be used (and is widely used) in the end of a method name. So if someone wrote something like:
It would be impossible to distinguish weather we were using the safe navigation operator or wanted to call the method
upcase on the result of
Overall, this feels like a good addition to the language. And I can't wait to start seeing it in the wild!