Analogue coffeescript of try () activesupport method

There is an extremely useful .try()rails method that helps me a lot in such functions:

hash.try(:a).try(:b)
# equal to
# if hash.present? && hash.a.present?
#   hash.a.b
# else
#   nil
# end

Is there something similar in coffeescript?

+4
source share
1 answer

Yes:

hash?.a?.b

See the existential operatorin the document

+10
source

All Articles