Issue #14123 has been updated by mame (Yusuke Endoh).


We discussed this issue at today's Ruby committer's meeting, and matz accepted the feature itself.

jeremyevans0 (Jeremy Evans) wrote:
> I request the implementation be thread-safe and that it fall back to `Kernel#p` if requiring `pp` fails.

Thank you for your opinion.

Okay, I move `undef pp` from `prelude.rb` to `lib/pp.rb` (See the last patch in detail).  The fatal race condition is fixed.
"Redefinition warnings" issue remains, but I think it is not significant because `pp` itself is "not so" thread-safe.  Simultaneous multiple calls to `pp` will interleave the outputs as below.  In principle, a user have to do exclusive control appropriately when calling `pp` (if s/he really cares).

```
$ ruby -rpp -e 'Thread.new { pp [1] * 10000 }; pp [2]*10000' | uniq
[1,
 1,
 1,[2,
 2,

 1,
 1, 2,
 2,

 1,
 1]
 2,
 2]
```

And, many in the meeting were against fallback to `p`.  This is just a useful shortcut for `pp`, so it should fail when `require "pp"` fails.

A revised patch:

```
diff --git a/lib/pp.rb b/lib/pp.rb
index 7d1c502817..0e737d23f6 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -17,6 +17,7 @@ def pretty_inspect
   # prints arguments in pretty form.
   #
   # pp returns argument(s).
+  undef pp if method_defined?(:pp)
   def pp(*objs)
     objs.each {|obj|
       PP.pp(obj)
diff --git a/prelude.rb b/prelude.rb
index 7b98e28285..3069fdbaf0 100644
--- a/prelude.rb
+++ b/prelude.rb
@@ -141,3 +141,10 @@ def irb
     irb
   end
 end
+
+module Kernel
+  def pp(*objs)
+    require 'pp'
+    pp(objs)
+  end
+end
```

I'll commit it in a few days.

----------------------------------------
Feature #14123: Kernel#pp by default
https://bugs.ruby-lang.org/issues/14123#change-68069

* Author: mame (Yusuke Endoh)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 2.5
----------------------------------------
Matz, may I commit this?  I really want this.

```
diff --git a/prelude.rb b/prelude.rb
index 7b98e28285..87f49ac9fb 100644
--- a/prelude.rb
+++ b/prelude.rb
@@ -141,3 +141,11 @@ def irb
     irb
   end
 end
+
+module Kernel
+  def pp(*objs)
+    undef :pp
+    require 'pp'
+    pp(*objs)
+  end
+end
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>