pp 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pp.rb +67 -28
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b437b9b1d9f113a93fc4ecda630db3c2a71ec7cb085c8637d77a57c04cda0d8c
4
- data.tar.gz: 3a68f16535169d4419d41aa9b4c4d265c2d8d43b0480d7a5b17fd9f3ef3230e0
3
+ metadata.gz: 8ae81116a5438a08aca95879913585f76a9d96603ad30bbd5b724479ba0adeca
4
+ data.tar.gz: 3d5bca0ec8f49203d50cd6bb386f0666895bc9cec73f18e3fe0118c1e24bf12a
5
5
  SHA512:
6
- metadata.gz: 6c00c899791aaacb3021bcab37ec02b58fa08a85352c6970ec6037a6bc7c2c01644c7c430a1e956fc569c5b2a290645e60e90e79ee4159f518d0c6491b098166
7
- data.tar.gz: 7ca2e413eaea277c0f6ac84ee4cc825bc71410a951133c64b7fde8e76924519dc3919598229e3c46d6d1527204b36b75376a76641ad55a54b43c14c694e2a683
6
+ metadata.gz: e843b0b52ef64b1b467b131968f39e9ec643f877459752c31affeec80184dc1ade4b1ce0b0a48a0c2e78a1fc29ca9f65355ec887b6ab4a50024edab0b0c4f525
7
+ data.tar.gz: 610bcf764e874ff0237a551d3d2bf2d1a4627c37eb2bba45fb1d64591b111c7693549c0cf22372599a3dbbbb27701f7754e50cc0fbc01f168a8cf8cc6aded6e0
data/lib/pp.rb CHANGED
@@ -64,7 +64,7 @@ require 'prettyprint'
64
64
  class PP < PrettyPrint
65
65
 
66
66
  # The version string
67
- VERSION = "0.6.2"
67
+ VERSION = "0.6.3"
68
68
 
69
69
  # Returns the usable width for +out+.
70
70
  # As the width of +out+:
@@ -145,21 +145,13 @@ class PP < PrettyPrint
145
145
  # Yields to a block
146
146
  # and preserves the previous set of objects being printed.
147
147
  def guard_inspect_key
148
- if Thread.current[:__recursive_key__] == nil
149
- Thread.current[:__recursive_key__] = {}.compare_by_identity
150
- end
151
-
152
- if Thread.current[:__recursive_key__][:inspect] == nil
153
- Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
154
- end
155
-
156
- save = Thread.current[:__recursive_key__][:inspect]
157
-
148
+ recursive_state = Thread.current[:__recursive_key__] ||= {}.compare_by_identity
149
+ save = recursive_state[:inspect] ||= {}.compare_by_identity
158
150
  begin
159
- Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
151
+ recursive_state[:inspect] = {}.compare_by_identity
160
152
  yield
161
153
  ensure
162
- Thread.current[:__recursive_key__][:inspect] = save
154
+ recursive_state[:inspect] = save
163
155
  end
164
156
  end
165
157
 
@@ -167,9 +159,8 @@ class PP < PrettyPrint
167
159
  # to be pretty printed. Used to break cycles in chains of objects to be
168
160
  # pretty printed.
169
161
  def check_inspect_key(id)
170
- Thread.current[:__recursive_key__] &&
171
- Thread.current[:__recursive_key__][:inspect] &&
172
- Thread.current[:__recursive_key__][:inspect].include?(id)
162
+ recursive_state = Thread.current[:__recursive_key__] or return false
163
+ recursive_state[:inspect]&.include?(id)
173
164
  end
174
165
 
175
166
  # Adds the object_id +id+ to the set of objects being pretty printed, so
@@ -183,6 +174,24 @@ class PP < PrettyPrint
183
174
  Thread.current[:__recursive_key__][:inspect].delete id
184
175
  end
185
176
 
177
+ private def guard_inspect(object)
178
+ recursive_state = Thread.current[:__recursive_key__]
179
+
180
+ if recursive_state&.key?(:inspect)
181
+ begin
182
+ push_inspect_key(object)
183
+ yield
184
+ ensure
185
+ pop_inspect_key(object) unless PP.sharing_detection
186
+ end
187
+ else
188
+ guard_inspect_key do
189
+ push_inspect_key(object)
190
+ yield
191
+ end
192
+ end
193
+ end
194
+
186
195
  # Adds +obj+ to the pretty printing buffer
187
196
  # using Object#pretty_print or Object#pretty_print_cycle.
188
197
  #
@@ -198,15 +207,12 @@ class PP < PrettyPrint
198
207
  return
199
208
  end
200
209
 
201
- begin
202
- push_inspect_key(obj)
210
+ guard_inspect(obj) do
203
211
  group do
204
212
  obj.pretty_print self
205
213
  rescue NoMethodError
206
214
  text Kernel.instance_method(:inspect).bind_call(obj)
207
215
  end
208
- ensure
209
- pop_inspect_key(obj) unless PP.sharing_detection
210
216
  end
211
217
  end
212
218
 
@@ -261,15 +267,20 @@ class PP < PrettyPrint
261
267
  def seplist(list, sep=nil, iter_method=:each) # :yield: element
262
268
  sep ||= lambda { comma_breakable }
263
269
  first = true
270
+ kwsplat = EMPTY_KWHASH
264
271
  list.__send__(iter_method) {|*v|
265
272
  if first
266
273
  first = false
267
274
  else
268
275
  sep.call
269
276
  end
270
- RUBY_VERSION >= "3.0" ? yield(*v, **{}) : yield(*v)
277
+ kwsplat ? yield(*v, **kwsplat) : yield(*v)
271
278
  }
272
279
  end
280
+ EMPTY_KWHASH = if RUBY_VERSION >= "3.0"
281
+ {}.freeze
282
+ end
283
+ private_constant :EMPTY_KWHASH
273
284
 
274
285
  # A present standard failsafe for pretty printing any given Object
275
286
  def pp_object(obj)
@@ -302,12 +313,10 @@ class PP < PrettyPrint
302
313
  # A pretty print for a pair of Hash
303
314
  def pp_hash_pair(k, v)
304
315
  if Symbol === k
305
- sym_s = k.inspect
306
- if sym_s[1].match?(/["$@!]/) || sym_s[-1].match?(/[%&*+\-\/<=>@\]^`|~]/)
307
- text "#{k.to_s.inspect}:"
308
- else
309
- text "#{k}:"
316
+ if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z])
317
+ k = k.to_s.inspect
310
318
  end
319
+ text "#{k}:"
311
320
  else
312
321
  pp k
313
322
  text ' '
@@ -379,7 +388,8 @@ class PP < PrettyPrint
379
388
  # This method should return an array of names of instance variables as symbols or strings as:
380
389
  # +[:@a, :@b]+.
381
390
  def pretty_print_instance_variables
382
- instance_variables.sort
391
+ ivars = respond_to?(:instance_variables_to_inspect) ? instance_variables_to_inspect : instance_variables
392
+ ivars.sort
383
393
  end
384
394
 
385
395
  # Is #inspect implementation using #pretty_print.
@@ -422,6 +432,28 @@ class Hash # :nodoc:
422
432
  end
423
433
  end
424
434
 
435
+ if defined?(Set)
436
+ if set_pp = Set.instance_method(:initialize).source_location
437
+ set_pp = !set_pp.first.end_with?("/set.rb") # not defined in set.rb
438
+ else
439
+ set_pp = true # defined in C
440
+ end
441
+ end
442
+ class Set # :nodoc:
443
+ def pretty_print(pp) # :nodoc:
444
+ pp.group(1, "#{self.class.name}[", ']') {
445
+ pp.seplist(self) { |o|
446
+ pp.pp o
447
+ }
448
+ }
449
+ end
450
+
451
+ def pretty_print_cycle(pp) # :nodoc:
452
+ name = self.class.name
453
+ pp.text(empty? ? "#{name}[]" : "#{name}[...]")
454
+ end
455
+ end if set_pp
456
+
425
457
  class << ENV # :nodoc:
426
458
  def pretty_print(q) # :nodoc:
427
459
  h = {}
@@ -452,6 +484,13 @@ class Struct # :nodoc:
452
484
  end
453
485
  end
454
486
 
487
+ verbose, $VERBOSE = $VERBOSE, nil
488
+ begin
489
+ has_data_define = defined?(Data.define)
490
+ ensure
491
+ $VERBOSE = verbose
492
+ end
493
+
455
494
  class Data # :nodoc:
456
495
  def pretty_print(q) # :nodoc:
457
496
  class_name = PP.mcall(self, Kernel, :class).name
@@ -484,7 +523,7 @@ class Data # :nodoc:
484
523
  def pretty_print_cycle(q) # :nodoc:
485
524
  q.text sprintf("#<data %s:...>", PP.mcall(self, Kernel, :class).name)
486
525
  end
487
- end if defined?(Data.define)
526
+ end if has_data_define
488
527
 
489
528
  class Range # :nodoc:
490
529
  def pretty_print(q) # :nodoc:
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanaka Akira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-03 00:00:00.000000000 Z
11
+ date: 2025-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prettyprint
@@ -42,7 +42,7 @@ licenses:
42
42
  metadata:
43
43
  homepage_uri: https://github.com/ruby/pp
44
44
  source_code_uri: https://github.com/ruby/pp
45
- post_install_message:
45
+ post_install_message:
46
46
  rdoc_options: []
47
47
  require_paths:
48
48
  - lib
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubygems_version: 3.5.11
61
- signing_key:
61
+ signing_key:
62
62
  specification_version: 4
63
63
  summary: Provides a PrettyPrinter for Ruby objects
64
64
  test_files: []