Which is the preferred way to determine if a chained call is nil? i.e:
company.andand.namecompany.try(:name)return nil if company.nil?; company.name(company or return nil).name
For the last one, you must use the predicate or rather than || to make the return work correctly.
4- Absolutely not. Too error prone.
3- I'd favour in cases where one can return early (which one should IMO)
1- Is typical in our monorail code base.
IMHO I'd usually try to be idiomatic, unless
andandactually reads well and shortens the syntax.If there is a default, I prefer:
(which is also faster) to:
If there isn't and returning
nilis fine,I would not use
trysince the semantics are different — ifcompanyis not nil but doesnt not respond to#nameyou actually want an exception to be raised.