Mike Griffin

Zebra striping a table with a ruby helper

I was trying to find a way to alternate the row colours in an HTML table for a Ruby application and stumbled across this DZone snippet.

The code in the snippet is:

  def alternate(str1 = "odd", str2 = "even")
   @alternate_odd_even_state = true if @alternate_odd_even_state.nil?
   @alternate_odd_even_state = !@alternate_odd_even_state
   @alternate_odd_even_state ? str2 : str1
  end

This is used in a HAML view with the following code:

%tr{:class => alternate('other', '')}