Equivalent of try() for hashes? - Ruby
To do a try() on hashes, its as simple as:
# to get data['something_value']
@item = @model.try(:data).try(:[], 'something_value']
2021 Update
- From
ruby 2.3.0
you can now do the following shorthand@model&.data&.[](:something_value)
- Reference: Safe navigation equivalent to Rails try for hashes
References: