Over a million developers have joined DZone.

Iterating Over A List

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 %>

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

{{ parent.tldr }}

{{ parent.urlSource.name }}