try

@anything.try(:any_method_or_attribute)
#=> nil 
# even Anything without any_method_or_attribute
@anything.try(:nil?)
# => nil

@anything.try(:blank?)
# => nil

@anything.nil?
# => true

@anything.blank?
# => true

# never use try for nil?, blank? or any methods implemeted by NilClass
# Using try for hash
h = { a: 1}
h.try(:[], :a)
# => 1
h.try(:[], :b)
# => nil

Reference

results matching ""

    No results matching ""