Search code examples
rubyconsolepretty-printirb

IRB and large variables?


How can I print a large variable nicely in an irb prompt? I have a variable that contains many variables which are long and the printout becomes a mess to wade through. What if I just want the variable names without their values? Or, can I print each one on a separate line, tabbed-in depending on depth?


Solution

  • Or, can I print each one on a separate line, tabbed-in depending on depth?

    Use pp (pretty print):

    require 'pp'
    very_long_hash = Hash[(1..23).zip(20..42)]
    pp very_long_hash
    # Prints:
    {1=>20,
     2=>21,
     3=>22,
     4=>23,
     5=>24,
     6=>25,
     7=>26,
     8=>27,
     9=>28,
     10=>29,
     11=>30,
     12=>31,
     13=>32,
     14=>33,
     15=>34,
     16=>35,
     17=>36,
     18=>37,
     19=>38,
     20=>39,
     21=>40,
     22=>41,
     23=>42}