Ruby try shortcut - Safe Navigation Operator
Today I learned that ruby (2.3.0) has an operator called Safe Navigation &.
and this is like a short cut for that Rails try method:
foo = nil
foo.bar
# NoMethodError: undefined method `bar` for nil:NilClass
# from (irb):18
foo&.bar
# => nil
class Foo
def bar
:barzzz
end
end
foo = Foo.new
foo.bar
# => :barzzz
foo&.bar
# => :barzzz
h/t @mattpolito
Tweet