I am looking for “elegant” way to check if given object is nil and it’s attribute is nil or empty. At the moment I have this check
response = foo.call() # some service call, no http code given :)
raise StandardError, "Response is not valid" if response.nil? || response['data'].nil? || reponse['data'].emtpy?
Is there more elegant way to do this, and avoid triple OR check? Wrap in begin/catch is not elegant way in case somebody would suggest that.
What about this?
As @ksol correctly mentions in the comments,
tryhelper comes from ActiveSupport. But it is not difficult at all to re-implement.Alternatives
Here’s Object#andand if you don’t want to drag along the entire activesupport and don’t feel like writing code that’s already written.