Skip to content

Commit da7f22b

Browse files
committed
supported INSTANCE_PPRINT
1 parent 1a30e9a commit da7f22b

8 files changed

Lines changed: 12126 additions & 23 deletions

File tree

‎v4-cokapi/backends/ruby/pg_logger.rb‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@
3333
# - rename 'function' to 'method'
3434
# - display private and protected attributes
3535
#
36-
# Useful notes from http://phrogz.net/programmingruby/frameset.html:
37-
# - "First, every object has a unique object identifier (abbreviated as
38-
# object id)."
39-
#
40-
# - display objects that have overridden to_s as INSTANCE_PPRINT using:
41-
# http://stackoverflow.com/questions/11462430/can-i-detect-that-a-method-has-been-overridden
42-
#
4336
# Limitations/quirks:
4437
# - no support for (lexical) environment pointers, since MRI doesn't seem to
4538
# expose them. We can see only the current (dynamic) stack backtrace
@@ -51,6 +44,11 @@
5144
#
5245
# - method aliases show up as separate Method objects instead of the
5346
# same one
47+
#
48+
# - if you write, say, "require 'date'", it will pull in classes from
49+
# the date module into globals, which might make the display HUGE
50+
# with way too many attributes. this is a general problem that
51+
# manifests in other languages as well.
5452

5553
# style guide: https://github.com/styleguide/ruby
5654

@@ -256,25 +254,29 @@ def encode(dat)
256254
new_obj.concat(encoded_class_variables)
257255
new_obj.concat(encoded_instance_variables)
258256
else
259-
new_obj << 'INSTANCE'
260-
new_obj << dat.class.to_s
257+
# concise case: instance that doesn't use the default Kernel to_s
258+
# (i.e., either it or its superclass defined a custom to_s):
259+
if dat.method(:to_s).owner != Kernel
260+
new_obj << 'INSTANCE_PPRINT'
261+
new_obj << dat.class.to_s
262+
new_obj << dat.to_s
263+
else
264+
new_obj << 'INSTANCE'
265+
new_obj << dat.class.to_s
261266

262-
# catch-all case: in Ruby, everything is an object
263-
my_inst_vars = dat.instance_variables
267+
# catch-all case: in Ruby, everything is an object
268+
my_inst_vars = dat.instance_variables
264269

265-
encoded_instance_variables = []
270+
encoded_instance_variables = []
266271

267-
my_inst_vars.each do |e|
268-
encoded_instance_variables << [e.to_s, encode(dat.instance_variable_get(e))]
269-
end
270-
271-
new_obj.concat(encoded_instance_variables)
272+
my_inst_vars.each do |e|
273+
encoded_instance_variables << [e.to_s, encode(dat.instance_variable_get(e))]
274+
end
272275

273-
# for brevity, put methods in the class, not in instances
276+
new_obj.concat(encoded_instance_variables)
274277

275-
# TODO: support pprint if to_s is custom-defined by this
276-
# instance, and not by the default Object or something
277-
# * instance with __str__ defined - ['INSTANCE_PPRINT', class name, <__str__ value>]
278+
# for brevity, put methods in the class, not in instances
279+
end
278280
end
279281

280282
return ret

0 commit comments

Comments
 (0)