1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
[](https://travis-ci.org/rocky/columnize) [](http://badge.fury.io/rb/columnize)
Columnize - Format an Array as a Column-aligned String
============================================================================
In showing a long lists, sometimes one would prefer to see the value
arranged aligned in columns. Some examples include listing methods of
an object, listing debugger commands, or showing a numeric array with data
aligned.
Setup
-----
$ irb
>> require 'columnize'
=> true
With numeric data
-----------------
>> a = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> a.columnize
=> "1 2 3 4 5 6 7 8 9 10"
>> puts a.columnize :arrange_array => true, :displaywidth => 10
[1, 2, 3,
4, 5, 6,
7, 8, 9,
10]
=> nil
>> puts a.columnize :arrange_array => true, :displaywidth => 20
[1, 2, 3, 4, 5, 6,
7, 8, 9, 10]
=> nil
With String data
----------------
>> g = %w(bibrons golden madascar leopard mourning suras tokay)
=> ["bibrons", "golden", "madascar", "leopard", "mourning", "suras", "tokay"]
>> puts g.columnize :displaywidth => 15
bibrons suras
golden tokay
madascar
leopard
mourning
=> nil
>> puts g.columnize :displaywidth => 19, :colsep => ' | '
bibrons | suras
golden | tokay
madascar
leopard
mourning
=> nil
>> puts g.columnize :displaywidth => 18, :colsep => ' | ', :ljust => false
bibrons | mourning
golden | suras
madascar | tokay
leopard
=> nil
Using Columnize.columnize
-------------------------
>> Columnize.columnize(a)
=> "1 2 3 4 5 6 7 8 9 10"
>> puts Columnize.columnize(a, :displaywidth => 10)
1 5 9
2 6 10
3 7
4 8
=> nil
>> Columnize.columnize(g)
=> "bibrons golden madascar leopard mourning suras tokay"
>> puts Columnize.columnize(g, :displaywidth => 19, :colsep => ' | ')
bibrons | mourning
golden | suras
madascar | tokay
leopard
=> nil
Credits
-------
This is adapted from a method of the same name from Python's cmd module.
Other stuff
-----------
Authors: Rocky Bernstein <rockyb@rubyforge.org> [](https://coderwall.com/rocky) and [Martin Davis](https://github.com/waslogic)
License: Copyright (c) 2011,2013 Rocky Bernstein
Warranty
--------
You can redistribute it and/or modify it under either the terms of the GPL
version 2 or the conditions listed in COPYING
|