Say I have my_obj object
and I get this object from the database, with some calculations, it doesn't matter ...
and then I need to iterate through an object like this:
my_obj.each do |m|
some_method(m.id)
end
this code is not good, because if my_obj is zero, I will get an error, for example:
undefined method `each' for nil:NilClass
so I decided to write:
if my_obj.present?
my_obj.each do |m|
some_method(m.id)
end
end
but I think there is another way to do this without attributing the construction everywhere.
so how can i iterate through an object only if it is not null?
source
share