Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easy Ruby interface to split a collection into columns, ideal as a view helper.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 autotest
Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Rakefile
Octocat-spinner-32 columns_for.gemspec


columns_for makes it easy to split a collection into columns. This fills a gaping hole in current CSS implementations.

If you float: left your data into columns, you'll end up with non-columnar sorting, like so:

Alpha    Bravo
Charlie  Delta
Echo     Foxtrot

… when really you want this:

Alpha    Delta
Bravo    Echo
Charlie  Foxtrot

… right? This is something that CSS should solve, and one day will.

Until then...

gem install columns_for
ColumnsFor::columns_for(@words).into(2) { |column| ... |

# or as a Rails 3 view helper...

echo 'gem "columns_for"' >> Gemfile
echo 'ActionView::Base.send :include, ColumnsFor' > config/initializers/columns_for.rb

<% columns_for(@words).into(2) do |column| %>
  <ul class="column column<%= column.number %>">
  <% for word in column %>
    <li><%= word %></li>
  <% end %>
<% end %>


$ rake spec
(in /Users/paul/code/ruby/columns_for)

  when mixed into a class
    responds to #columns_for
    returns instance of ColumnsFor::Columnizer
    responds to #into
    the #into method yields
      yields 0 times for 0 columns
      yields 1 times for 1 columns
      yields 2 times for 2 columns
      yields 3 times for 3 columns
    the #into method splits
      splits 1 items into 1 columns: 1
      splits 2 items into 1 columns: 2
      splits 1 items into 2 columns: 1, 0
      splits 2 items into 2 columns: 1, 1
      splits 10 items into 2 columns: 5, 5
      splits 10 items into 3 columns: 4, 4, 2
      splits 10 items into 4 columns: 3, 3, 3, 1
      splits 11 items into 3 columns: 4, 4, 3

Finished in 0.00641 seconds
15 examples, 0 failures


© 2010 Paul Annesley, MIT license.

Something went wrong with that request. Please try again.