Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better alternative to try(:output).try(:data).try(:name)?

"output" is a serialized OpenStruct.

def title
  try(:output).try(:data).try(:title)
end

What would be better? :)

like image 672
Jay Avatar asked Sep 26 '09 07:09

Jay


2 Answers

Or simply this:

def title
  output.data.title rescue nil
end
like image 50
Benjamin Curtis Avatar answered Oct 03 '22 07:10

Benjamin Curtis


Referring to this blog you might find it better to use the &. operator like below for ruby version > 2.3.0;

 output&.data&.title
like image 39
Olalekan Sogunle Avatar answered Oct 03 '22 07:10

Olalekan Sogunle