I have a few places in my code where I have nested objects, but can’t guarantee they will always be set.
This could give me an ruby undefined method for nil:NilClass
puts obj1.obj2.obj3.obj4.to_s
This check is ugly and repetitive:
if(obj1 && obj1.obj2 && obj1.obj2.obj3 && obj1.obj2.obj3.obj4)
puts obj1.obj2.obj3.obj4.to_s
end
Is there a concise way to write, if anything is nil just silently fail?
My version of try:
So then instead of this long expression:
you can do this:
UPDATE:
Made it a little cleaner