1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/ruby/safe-navigation-operator.md
2015-12-27 09:54:04 -06:00

26 lines
725 B
Markdown

# Safe Navigation Operator
With the release of Ruby 2.3, the *safe navigation operator* (`&.`) is now
available. This addition to the Ruby language allows you to collapse all
those pesky `nil` checks into the accessor call they are guarding. Consider
this snippet of common Ruby code:
```ruby
if user && user.authenticate(params[:password])
# proceed with logged in user
end
```
With the *safe navigation operator*, the predicate can now be collapsed:
```ruby
if user&.authenticate(params[:password])
# proceed with logged in user
end
```
If `user` is `nil`, then the predicate will evaluate to `false` and the body
of the if-statement will be passed over.
[Source](http://nithinbekal.com/posts/ruby-2-3-features/)