Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Ruby's prettyprint

Is it possible to change the width that prettyprint (require 'pp') uses when formatting output? For example:

"mooth"=>["booth", "month", "mooch", "morth", "mouth", "mowth", "sooth", "tooth"]
"morth"=>["forth",
 "mirth",
 "month",
 "mooth",
 "morph",
 "mouth",
 "mowth",
 "north",
 "worth"]

The first array is printed inline because it fits within the column width prettyprint allows (79 characters)... the second is split onto multiple lines, because it does not. But I can find no method for changing the column that this behavior starts on.

pp depends on PrettyPrint (which has ways to allow different widths for the buffer). Is there any way to change the default column width for pp, without rewriting it from scratch (accessing PrettyPrint directly)?

Alternately, is there a similar ruby gem that provides this functionality?

like image 382
Myrddin Emrys Avatar asked Jan 21 '10 19:01

Myrddin Emrys


People also ask

How do you make a pretty print in Ruby?

To define a customized pretty printing function for your classes, redefine method #pretty_print(pp) in the class. #pretty_print takes the pp argument, which is an instance of the PP class. The method uses text , breakable , nest , group and pp to print the object.

Which Ruby object method controls how an object is displayed in the console?

irb - Method called on an object when displayed on the console in Ruby - Stack Overflow.


2 Answers

#!/usr/bin/ruby1.8  require 'pp' mooth = [   "booth", "month", "mooch", "morth",   "mouth", "mowth", "sooth", "tooth" ] PP.pp(mooth, $>, 40) # => ["booth", # =>  "month", # =>  "mooch", # =>  "morth", # =>  "mouth", # =>  "mowth", # =>  "sooth", # =>  "tooth"] PP.pp(mooth, $>, 79) # => ["booth", "month", "mooch", "morth", "mouth", "mowth", "sooth", "tooth"] 

To change the default with a monkey patch:

#!/usr/bin/ruby1.8  require 'pp'  class PP   class << self     alias_method :old_pp, :pp     def pp(obj, out = $>, width = 40)       old_pp(obj, out, width)     end   end end  mooth = ["booth", "month", "mooch", "morth", "mouth", "mowth", "sooth", "tooth"] pp(mooth) # => ["booth", # =>  "month", # =>  "mooch", # =>  "morth", # =>  "mouth", # =>  "mowth", # =>  "sooth", # =>  "tooth"] 

These methods also work in MRI 1.9.3

like image 71
Wayne Conrad Avatar answered Sep 29 '22 06:09

Wayne Conrad


Found "ap" aka "Awesome_Print" useful as well from git-repo

Code used to test pp and ap:

require 'pp'
require "awesome_print" #requires gem install awesome_print 

data = [false, 42, %w{fourty two}, {:now => Time.now, :class => Time.now.class, :distance => 42e42}]
puts "Data displayed using pp command"
pp data

puts "Data displayed using ap command"
ap data

O/P from pp vs ap:

Data displayed using pp command
[false,
 42,
 ["fourty", "two"],
 {:now=>2015-09-29 22:39:13 +0800, :class=>Time, :distance=>4.2e+43}]

Data displayed using ap command
[
    [0] false,
    [1] 42,
    [2] [
        [0] "fourty",
        [1] "two"
    ],
    [3] {
             :now => 2015-09-29 22:39:13 +0800,
           :class => Time < Object,
        :distance => 4.2e+43
    }
]

Reference:

  • Stackoverflow posting
  • Web citing
like image 21
3 revs, 3 users 98% Avatar answered Sep 29 '22 04:09

3 revs, 3 users 98%