ruby-changes:65587
From: Takashi <ko1@a...>
Date: Sat, 20 Mar 2021 20:25:47 +0900 (JST)
Subject: [ruby-changes:65587] 278522f053 (master): [ruby/irb] Don't call Ruby 2.4+'s String#pretty_print
https://git.ruby-lang.org/ruby.git/commit/?id=278522f053 From 278522f0535d081ff6473f210cb45f923ff8810a Mon Sep 17 00:00:00 2001 From: Takashi Kokubun <takashikkbn@g...> Date: Fri, 19 Mar 2021 19:08:39 -0700 Subject: [ruby/irb] Don't call Ruby 2.4+'s String#pretty_print https://github.com/ruby/irb/commit/89bcf107be --- lib/irb/color_printer.rb | 9 +++++++++ test/irb/test_color_printer.rb | 1 + 2 files changed, 10 insertions(+) diff --git a/lib/irb/color_printer.rb b/lib/irb/color_printer.rb index 92afea5..30c6825 100644 --- a/lib/irb/color_printer.rb +++ b/lib/irb/color_printer.rb @@ -21,6 +21,15 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/color_printer.rb#L21 end end + def pp(obj) + if obj.is_a?(String) + # Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n" + text(obj.inspect) + else + super + end + end + def text(str, width = nil) unless str.is_a?(String) str = str.inspect diff --git a/test/irb/test_color_printer.rb b/test/irb/test_color_printer.rb index 1b28837..1afc7cc 100644 --- a/test/irb/test_color_printer.rb +++ b/test/irb/test_color_printer.rb @@ -34,6 +34,7 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color_printer.rb#L34 end { 1 => "#{BLUE}#{BOLD}1#{CLEAR}\n", + "a\nb" => %[#{RED}#{BOLD}"#{CLEAR}#{RED}a\\nb#{CLEAR}#{RED}#{BOLD}"#{CLEAR}\n], IRBTestColorPrinter.new('test') => "#{GREEN}#<struct TestIRB::TestColorPrinter::IRBTestColorPrinter#{CLEAR} a#{GREEN}=#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{RED}test#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}\n", Ripper::Lexer.new('1').scan => "[#{GREEN}#<Ripper::Lexer::Elem:#{CLEAR} on_int@1:0 END token: #{RED}#{BOLD}\"#{CLEAR}#{RED}1#{CLEAR}#{RED}#{BOLD}\"#{CLEAR}#{GREEN}>#{CLEAR}]\n", Class.new{define_method(:pretty_print){|q| q.text("[__FILE__, __LINE__, __ENCODING__]")}}.new => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}, #{CYAN}#{BOLD}__ENCODING__#{CLEAR}]\n", -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/