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:

&lt;% @hints.each do |hint| %&gt;
    &lt;% unless hint.empty? %&gt;
        &lt;p&gt;Please observe the following hints:&lt;/p&gt;

        &lt;ul&gt;
        &lt;% @hints.each do |hint| %&gt;
            &lt;% unless hint.empty? %&gt;
                &lt;li&gt;&lt;%= hint %&gt;&lt;/li&gt;
            &lt;% end %&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;

        &lt;% break %&gt;
    &lt;% end %&gt;
&lt;% end %&gt;


An example of @hints:

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


Or:

[""]


Anyone know a better way?
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?(&amp;:present)


With that you could do something like

&lt;% if @hints.any?(&amp;:present) %&gt;
    &lt;p&gt;Please observe the following hints:&lt;/p&gt;

    &lt;ul&gt;
    &lt;% @hints.each do |hint| %&gt;
        &lt;% unless hint.empty? %&gt;
            &lt;li&gt;&lt;%= hint %&gt;&lt;/li&gt;
        &lt;% end %&gt;
    &lt;% end %&gt;
    &lt;/ul&gt;
&lt;% end %&gt;


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(&amp;:blank?)   # reject blank strings
# or...
@hints = some_hints.select(&amp;: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:

&lt;% unless @hints.empty? %&gt;
    &lt;p&gt;Please observe the following hints:&lt;/p&gt;

    &lt;ul&gt;
    &lt;% @hints.each do |hint| %&gt;
        &lt;li&gt;&lt;%= hint %&gt;&lt;/li&gt;
    &lt;% end %&gt;
    &lt;/ul&gt;
&lt;% end %&gt;