What, if any, are the differences between the ruby 2.3 safe operator &.
and the CoffeeScript existential operator .?
CoffeeScript's existential operator
?
returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?
Instead, the new Ruby safe navigation operator &.
is used to call a method of an Object
that can be nil
without raising an exception. If the object is not nil
, the method will be executed; otherwise, it returns nil
.
Something like this:
obj.try!(:method1).try!(:method2)
if obj && obj.method1
#...
end
becomes:
obj&.method1&.method2
if obj&.method1
#...
end
References: