Is there a way to use .each
so it does not throw an error if the object is nil or empty (without adding an additional nil/blank test?
It seems that if I say phonelist.each do |phone|
that if phonelist is empty, then the block should not be executed.
But in my view (haml) I have - @myvar.phonelist.each do |phone|
and if phonelist is empty, it throws a NoMethodError.
I run into this a lot, and always workaround by adding an explicit check/branch for .blank? but it seems there should be an easier way to tell .each that empty means do nothing.
each
on an empty enumerable should just do nothing. – Predisposeeach
onnil
. – Ethiopnil
object, not an empty collection. – Zeller