1.I can't find an elegant way to write this code:
if array.empty?
# process empty array
else
array.each do |el|
# process el
end
end
I'd like to have one loop, without writing array
twice. I read this, but there is no solution good enough.
2. I am actually in an HAML template. Same question.
- if array.empty?
%p No result
- else
%ul
- array.each do |el|
%li el
If
array
is empty, then it will not be iterated, so theeach
block does not need to be conditioned. Since the return value ofeach
is the receiver, you can put theeach
block within theempty?
condition.