Sorry, there are no things
You know when there's something in the back of your mind, something you want to check out that you never get around to? Here's one from why that just made my day!
things = [1,2,3] if things.each do |thing| puts thing end.empty? then puts "no things!" end
It takes the classic case of if there's things, use the things, otherwise do something completely different and makes it beautiful. This is extra handy in ERB since there's only three lines of logic instead of five. (Note using the for..in alternative as well).
<% if for thing in things %> <%= thing %> <% end.empty? then %> no things <% end %>
Bonus points for anyone that can write the following more simply...
<% unless things.empty? %> <ul> <% for thing in things %> <li><%= thing %></li> <% end %> </ul> <% else %> no things <% end %>
There are 4 comments
Note that the "then" in your first two examples is extraneous. No help on the third though. :-)
Depends on what you consider more simply I guess.
<%= things.empty? ? 'NO THINGS' : contenttag('ul', things.collect { |thing| contenttag('li', thing) }) %>
Personally, I find the example a little wordy and obfuscatory. If you expect the block to have side-effects, as in this example, the following is far easier to stuff in ERB:
puts 'no things' if things.reject {|thing| puts thing}.empty?
@andrew: the 'then' helps with readability - see flgr's comment on the redhanded post
Assuming the existence of an array helper method, you could do: <%= things.empty?? "no things" : things.to_html %>