As @tokland pointed out, your current code has one too many loops really.
As for checking if all the strings are empty, you have some options.
1) The any?
method accepts a block, so you can test if any of the elements in an array match a certain condition. In your case, you could do
@hints.any? { |string| string.present? }
since present?
checks if a string is non-empty. But you can also shorten that to just
@hints.any?(&:present)
With that you could do something like
<% if @hints.any?(&:present) %>
<p>Please observe the following hints:</p>
<ul>
<% @hints.each do |hint| %>
<% unless hint.empty? %>
<li><%= hint %></li>
<% end %>
<% end %>
</ul>
<% end %>
2) Alternatively, and I'd say nicer, you can filter the array beforehand, to simply not include blank strings using select
or, conversely, reject
.
@hints = some_hints.reject(&:blank?) # reject blank strings
# or...
@hints = some_hints.select(&:present?) # keep non-blank strings
This should happen in the controller when you first set @ hints
(or even earlier - don't know where the hints come from, but there's little need to keep empty strings around if you don't have to, so either strip them out early, or just avoid adding them to begin with).
With a "clean" @hints
array you can do this:
<% unless @hints.empty? %>
<p>Please observe the following hints:</p>
<ul>
<% @hints.each do |hint| %>
<li><%= hint %></li>
<% end %>
</ul>
<% end %>
@hints.each
... \$\endgroup\$@hints.each
gets stopped as soon as a non-empty string is detected so in theory the nested@hints.each
only gets ran once. Oh, I just noticed how that secondunless hint.empty?
is not necessary. It works though. \$\endgroup\$