Over a million developers have joined DZone.

Iterating Over A List

DZone's Guide to

Iterating Over A List

Free Resource
One of the common things you want to do with a table in a web application is use different classes so the rows have different styles.  i.e alternate between light and dark backgrounds.

Arry#cycle is a method to enable that.

class Array
  def cycle(values)
    self.each_with_index do |o, i| 
      yield(o, values[i % values.length])

You can use it like this:

<% @something.cycle(["oddRow", "evenRow"]) do |obj, cssClass| %>
    <%= obj.something %>
    <%= obj.something_else %>
<% end %>

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}