From ea8990d8670f71d6b1fa3d55086c9f147348452c Mon Sep 17 00:00:00 2001 From: akr Date: Tue, 11 Oct 2016 07:44:58 +0000 Subject: [PATCH] pp prints a multiple lines string prettier. * lib/pp.rb (String#pretty_print): Defined to print a string as multiple lines. [ruby-core:76800] [Feature#12664] proposed by Petr Chalupa. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56391 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ lib/pp.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2fb6ec3b65..40ac5ffe0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Oct 11 16:38:32 2016 Tanaka Akira + + * lib/pp.rb (String#pretty_print): Defined to print a string as + multiple lines. + [ruby-core:76800] [Feature#12664] proposed by Petr Chalupa. + Mon Oct 10 15:22:27 2016 Nobuyoshi Nakada * ruby.c (open_load_file): bind the open fd to an IO instance diff --git a/lib/pp.rb b/lib/pp.rb index 9678774b8c..5356b7ce25 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -418,6 +418,21 @@ class Range # :nodoc: end end +class String + def pretty_print(q) + lines = self.lines + if lines.size > 1 + q.group(0, '', '') do + q.seplist(lines, lambda { q.text ' +'; q.breakable }) do |v| + q.pp v + end + end + else + q.text inspect + end + end +end + class File < IO # :nodoc: class Stat # :nodoc: def pretty_print(q) # :nodoc: