How do I avoid doing this?
if boolean_array[day] && boolean_array[day][slot] && boolean_array[day][slot].zero?
# boolean_array[day][slot] element exists
end
Basically, you want an andand method. You can then do if boolean_array[day].andand[slot].andand.zero?.
Raganwald has one popular implementation.
I like Chuck's andand. I suppose you could also use the low-priority and to do it in plain Ruby, at least there would be no parens:
>> day = slot = 1; boolean_array = [[], [1,2]]
>> if t = boolean_array[day] and t = t[slot] and t = t.class
>> puts t
>> end
Fixnum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With