Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript analog of try() activesupport method

There is extremely useful .try() rails method, which helps me a lot with functions like that:

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

Is there anything similar to that in coffeescript?

like image 926
asiniy Avatar asked Dec 24 '22 18:12

asiniy


1 Answers

Yes:

hash?.a?.b

See the existential operator in the doc

like image 77
apneadiving Avatar answered Jan 09 '23 11:01

apneadiving