Skip to content

Instantly share code, notes, and snippets.

@pjkelly
Created September 10, 2009 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjkelly/184220 to your computer and use it in GitHub Desktop.
Save pjkelly/184220 to your computer and use it in GitHub Desktop.
module Columnizer
def columnize(k=3)
result_set = []
n = self.index(self.last)
r = (n + 1).to_f / k.to_f
(0..n).each do |z|
x = (z.to_f * r).ceil
x = x - n while x > n
result_set << self[x]
end
result_set
end
def columnize!(k=3)
self.replace columnize(k)
end
end
class Array
include Columnizer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment