Ruby Set IRB to Pretty_Inspect by default

I am new to ruby ​​and am setting up IRB. I like the pretty print (requires "pp"), but it seems like the hassle is always typing pp so it can print it. What I would like to do is make it pretty printable by default, so if I have var, say "myvar" and type myvar, it automatically calls pretty_inspect instead of the usual check. Where to begin? Ideally, I could add a method to my .irbrc file, which is automatically called. Any ideas?

Thanks!

+5
source share
2 answers

Pretty default print objects in irb is exactly what hirb needs to do . These posts explain how hirb can convert almost anything to an ascii table. Although hirb needs to be configured for each class, you could display all objects in tables:

# put this in ~/.irbrc
require 'rubygems'
require 'hirb'
Hirb.enable :output=>{'Object'=>{:class=>:auto_table, :ancestor=>true}}

# in irb
>> %w{three blind mice}
+-------+
| value |
+-------+
| three |
| blind |
| mice  |
+-------+
3 rows in set

>> 1..5
+-------+
| value |
+-------+
| 1     |
| 2     |
| 3     |
| 4     |
| 5     |
+-------+
5 rows in set

>> {:a=>1, :b=>2}
+---+---+
| 0 | 1 |
+---+---+
| a | 1 |
| b | 2 |
+---+---+
2 rows in set

This overflow-related solution also has an example of hirb in action.

+9
source

when irb starts, it reads .irbrc from your $ HOME directory. If you are editing (or creating) this file and adding

require 'pp'

it will load every time irb starts.

Check this box for pretty_print method lists. Throw this into your .irbrc and you can do:

>> 5.pm
                      %(arg1)         Fixnum
                      &(arg1)         Fixnum
                      *(arg1)         Fixnum
                     **(arg1)         Fixnum
                      +(arg1)         Fixnum
                     +@()             Fixnum(Numeric)
                      -(arg1)         Fixnum
                     -@()             Fixnum
                      /(arg1)         Fixnum
                     <<(arg1)         Fixnum
                     >>(arg1)         Fixnum
                     [](arg1)         Fixnum
                      ^(arg1)         Fixnum
                    abs()             Fixnum
                    ago(arg1, ...)    Fixnum(ActiveSupport::CoreExtensions::Numeric::Time)
               between?(arg1, arg2)   Fixnum(Comparable)
                   byte()             Fixnum(ActiveSupport::CoreExtensions::Numeric::Bytes)
                  bytes()             Fixnum(ActiveSupport::CoreExtensions::Numeric::Bytes

+1

All Articles