From: mame@... Date: 2021-06-07T07:16:48+00:00 Subject: [ruby-core:104184] [Ruby master Feature#12913] A way to configure the default maximum width of pp Issue #12913 has been updated by mame (Yusuke Endoh). So, some people (including @ko1) have a custom to pass keyword arguments to Kernel#pp: ``` foo = 42 bar = 43 pp(foo: foo, bar: bar) #=> {:foo=>42, :bar=>43} ``` Unfortunately, introducing keyword arguments to `Kernel#pp` breaks their custom. How about introducing `pp_out` and `pp_width` keywords? It is a bit dirty, but good enoguh. ```diff diff --git a/lib/pp.rb b/lib/pp.rb index 72480e5304..b090d2bdeb 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -91,6 +91,15 @@ def PP.singleline_pp(obj, out=$>) def PP.mcall(obj, mod, meth, *args, &block) mod.instance_method(meth).bind_call(obj, *args, &block) end + + def PP.width_for(out) + begin + require 'io/console' + _, width = out.winsize + rescue LoadError, NoMethodError, Errno::ENOTTY + end + (width || ENV['COLUMNS']&.to_i&.nonzero? || 80) - 1 + end # :startdoc: if defined? ::Ractor @@ -596,12 +605,21 @@ def pretty_inspect PP.pp(self, ''.dup) end - # prints arguments in pretty form. + # Prints arguments in pretty form. # # pp returns argument(s). - def pp(*objs) + # + # It prints to +$>+ by default. A keyword argument +pp_out+ is given, + # it is used as the output target. + # + # The output is fitted to the width of the console by default. + # A keyword +pp_width+ is used if given. + def pp(*objs, **kw) + out = kw.delete(:pp_out) || $> + width = kw.delete(:pp_width) || PP.width_for(out) + objs << kw unless kw.empty? objs.each {|obj| - PP.pp(obj) + PP.pp(obj, out, width) } objs.size <= 1 ? objs.first : objs end diff --git a/prelude.rb b/prelude.rb index b1e477a3ea..b8c564e84e 100644 --- a/prelude.rb +++ b/prelude.rb @@ -10,9 +10,9 @@ def irb end module Kernel - def pp(*objs) + def pp(...) require 'pp' - pp(*objs) + pp(...) end # suppress redefinition warning ``` ---------------------------------------- Feature #12913: A way to configure the default maximum width of pp https://bugs.ruby-lang.org/issues/12913#change-92364 * Author: mame (Yusuke Endoh) * Status: Assigned * Priority: Normal * Assignee: nobu (Nobuyoshi Nakada) ---------------------------------------- How about having an easy way to configure the maximum width of a line of `pp` output? Currently, `pp` accepts the maximum width as an optional argument: pp(big_array, $>, 120) However, this is obviously too long for a useful debugging-purpose method like `pp`. Even worse, we must add the fragment "`, $>, 120`" to all calls to `pp`. I don't feel this is reasonable. The patch attached provides `PP.default_maxwidth=` and `PP.default_maxwidth`, which can be used to configure the default setting of the maxwidth. PP.default_maxwidth = 1 pp([1, 2, 3]) #=> [1, # 2, # 3] Akr-san, what do you think? ---Files-------------------------------- pp-default-maxwidth.patch (1.05 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: