Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dave Dribin http://www.dribin.org/dave/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Yet another method to alternate table row classes

Use a helper function/class as such:

   1  <%- row_class = cycle("even", "odd") -%>
   2  <%- for item in @items do -%>
   3    <tr class="<%= row_class %>">
   4      ... use item ...
   5    </tr>
   6  <%- end -%>


Put this in your application_helper.rb:

   1    def cycle(first_value, *values)
   2      values.unshift(first_value)
   3      return Cycle.new(*values)
   4    end
   5  
   6    class Cycle
   7      def initialize(first_value, *values)
   8        @values = values.unshift(first_value)
   9        @index = 0
  10      end
  11  
  12      def to_s
  13        value = @values[@index].to_s
  14        @index = (@index + 1) % @values.size
  15        return value
  16      end
  17    end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS