|
33 | 33 | # - rename 'function' to 'method' |
34 | 34 | # - display private and protected attributes |
35 | 35 | # |
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 | | -# |
43 | 36 | # Limitations/quirks: |
44 | 37 | # - no support for (lexical) environment pointers, since MRI doesn't seem to |
45 | 38 | # expose them. We can see only the current (dynamic) stack backtrace |
|
51 | 44 | # |
52 | 45 | # - method aliases show up as separate Method objects instead of the |
53 | 46 | # 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. |
54 | 52 |
|
55 | 53 | # style guide: https://github.com/styleguide/ruby |
56 | 54 |
|
@@ -256,25 +254,29 @@ def encode(dat) |
256 | 254 | new_obj.concat(encoded_class_variables) |
257 | 255 | new_obj.concat(encoded_instance_variables) |
258 | 256 | 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 |
261 | 266 |
|
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 |
264 | 269 |
|
265 | | - encoded_instance_variables = [] |
| 270 | + encoded_instance_variables = [] |
266 | 271 |
|
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 |
272 | 275 |
|
273 | | - # for brevity, put methods in the class, not in instances |
| 276 | + new_obj.concat(encoded_instance_variables) |
274 | 277 |
|
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 |
278 | 280 | end |
279 | 281 |
|
280 | 282 | return ret |
|
0 commit comments