Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. class String
  2. # reformats a list (newline separated) into columns
  3. def columnize(columns=2, orientation="vertical", prepend="")
  4. list = self.split("\n")
  5. sizes = []
  6. out = ""
  7. items = (list.length.to_f/columns.to_f).ceil #items per column
  8. orientation = (orientation[0,1].downcase == "v")
  9. if (orientation) then
  10. 0.upto(columns-1) { |col|
  11. 0.upto(items-1) { |row|
  12. item = col*items+row
  13. list[item] = list[item].nil? ? "" : prepend+list[item]
  14. sizes[col] = [sizes[col], list[item].length].max
  15. }
  16. }
  17. 0.upto(items-1) { |row|
  18. 0.upto(columns-1) { |col|
  19. item = col*items+row
  20. out = out+list[item]+" "*(((sizes[col]-list[item].length)+2))
  21. }
  22. out = out+"\n"
  23. }
  24. else
  25. 0.upto(columns-1) { |col|
  26. 0.upto(items-1) { |row|
  27. item = row*columns+col
  28. list[item] = list[item].nil? ? "" : prepend+list[item]
  29. sizes[col] = [sizes[col], list[item].length].max
  30. }
  31. }
  32. 0.upto(items-1) { |row|
  33. 0.upto(columns-1) { |col|
  34. item = row*columns+col
  35. out = out+list[item]+" "*(((sizes[col]-list[item].length)+2))
  36. }
  37. out = out+"\n"
  38. }
  39. end
  40. return out[0..-2]
  41. end
  42. end
Add Comment
Please, Sign In to add comment