Platinum Partner

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])
    end
  end
end


You can use it like this:


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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}