Created by Xander Miller / @pareidoliax
New to Ruby 2.3
Shorthand for a common pattern
Similar to Rails try(:method)
Might be confused with &block
Often to avoid a no method exception we use this pattern
current_user && current_user.name
The safe navigation operator shortens this to one operation
current_user&.name
Safe Navigation passes forward nil while the Rails try(:method) catches any exception thrown by the method call.
This returns nil with .try()
42.try(:first)
but throws a no method exception when using safe navigation
42&.first
This makes safe naviagation useful for situations in Rails where you just want to handle the possiblity of a nil object.
Safe Navigator operator makes sense but could be confused with the way Ruby handles named blocks
def speak(&block) puts block.call end
or
[1, 1, 1].map(& :to_s)
This makes safe naviagation useful for situations in Rails where you just want to handle the possiblity of a nil object.
class ContactPolicy < ApplicationPolicy def show? user.present? && [:admin, :manager, :support, :customer].include?(user.role) end def new? show? end def create? new? end def edit? user.present? && [:admin, :manager, :support].include?(user.role) end def update? show? end def delete? user.present? && [:admin].include?(user.role) end end
def edit? user.present? && [:admin, :manager, :support].include?(user.role) end
becomes
def edit? [:admin, :manager, :support].include?(user&.role) end