Enable hirb print for array

489 Views Asked by At

In hirb tutorial there's an example for array:

[[1,2], [2,3]] 

prints as

+---+---+
| 0 | 1 |
+---+---+
| 1 | 2 |
| 2 | 3 |
+---+---+

But I can't make it work after usual setting up:

require 'hirb'
=>true
>> Hirb.enable
=>nil

The arrays are still printed in usuall manner.

What does it require to print an array as a table?

2

There are 2 best solutions below

1
On BEST ANSWER

This works for me:

irb(main):001:0> require 'hirb'
=> true
irb(main):002:0> Hirb.enable :output=>{"Array"=>{:class=>Hirb::Helpers::Table}}
=> true
irb(main):003:0> [[1,2], [2,3]]
+---+---+
| 0 | 1 |
+---+---+
| 1 | 2 |
| 2 | 3 |
+---+---+
2 rows in set
0
On

As an alternative to the above, this works well also, via github user: pjb3

add this to your gemfile:

group :development do
  gem 'hirb'
end

Run bundle to get the gem:

$ bundle

Modify your ~/.irbrc to enable HIRB:

$ echo 'begin; require "hirb"; Hirb::View.enable; rescue; end' >> ~/.irbrc