From 81fc96f73bdb26901c75838262779002bf6e14c5 Mon Sep 17 00:00:00 2001 From: rpag Date: Mon, 12 Jan 2015 20:18:59 +0000 Subject: [PATCH] support custom implementation of BasicObject#inspect. If BasicObject or a subclass of BasicObject implements #inspect, we will try to use its return value as output but if that fails, we'll fallback on __id__. fixes #1341 --- lib/pry/color_printer.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/pry/color_printer.rb b/lib/pry/color_printer.rb index 218a821..48bcc1a 100644 --- a/lib/pry/color_printer.rb +++ b/lib/pry/color_printer.rb @@ -34,14 +34,17 @@ def pp(obj) super rescue => e raise if e.is_a? Pry::Pager::StopPaging - - # Read the class name off of the singleton class to provide a default - # inspect. - singleton = class << obj; self; end - ancestors = Pry::Method.safe_send(singleton, :ancestors) - klass = ancestors.reject { |k| k == singleton }.first - obj_id = obj.__id__.to_s(16) rescue 0 - str = "#<#{klass}:0x#{obj_id}>" + begin + str = obj.inspect + rescue Exception + # Read the class name off of the singleton class to provide a default + # inspect. + singleton = class << obj; self; end + ancestors = Pry::Method.safe_send(singleton, :ancestors) + klass = ancestors.reject { |k| k == singleton }.first + obj_id = obj.__id__.to_s(16) rescue 0 + str = "#<#{klass}:0x#{obj_id}>" + end text highlight_object_literal(str) end