Search code examples
rubypretty-print

Is there a built-in method or gem that can print a Ruby array in columns?


I have a large Ruby array that I would like to print in columns, just like the default output of Unix' 'ls' command (on OS X, at least). Is there a gem or built-in method that can do this? I am aware of the awesome_print gem. It's got some sexy output but it doesn't seem to offer columns.


Solution

  • I think hirb is more useful:

    require 'hirb'
    
    Hirb.enable :output=>{"Array"=>{:class=>Hirb::Helpers::Table}}  #=> true
    
    [[1,2], [2,3]]
    +---+---+
    | 0 | 1 |
    +---+---+
    | 1 | 2 |
    | 2 | 3 |
    +---+---+
    2 rows in set
    
    [[5,6,3,4]]
    +---+---+---+---+
    | 0 | 1 | 2 | 3 |
    +---+---+---+---+
    | 5 | 6 | 3 | 4 |
    +---+---+---+---+
    1 row in set