Skip to main content

Hide content unless an array contains at least one non-empty string

I have an array of hints. Sometimes, this array could contain an empty string. Occasionally, the array might contain only empty strings, in which case I want the whole block to be skipped. This is what I came up with:

<% @hints.each do |hint| %>
    <% unless hint.empty? %>
        <p>Please observe the following hints:</p>

        <ul>
        <% @hints.each do |hint| %>
            <% unless hint.empty? %>
                <li><%= hint %></li>
            <% end %>
        <% end %>
        </ul>
        
        <% break %>
    <% end %>
<% end %>

An example of @hints:

["", "", "Have you tried turning it off and on again?"]

Or:

[""]

Anyone know a better way?

Phasy
  • 65
  • 3