summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/pp.rb4
-rw-r--r--test/test_pp.rb12
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/pp.rb b/lib/pp.rb
index 2cfc2c4..81a9a16 100644
--- a/lib/pp.rb
+++ b/lib/pp.rb
@@ -149,6 +149,10 @@ class PP < PrettyPrint
# Object#pretty_print_cycle is used when +obj+ is already
# printed, a.k.a the object reference chain has a cycle.
def pp(obj)
+ # If obj is a Delegator then use the object being delegated to for cycle
+ # detection
+ obj = obj.__getobj__ if defined?(::Delegator) and obj.is_a?(::Delegator)
+
if check_inspect_key(obj)
group {obj.pretty_print_cycle self}
return
diff --git a/test/test_pp.rb b/test/test_pp.rb
index 4736bff..3262417 100644
--- a/test/test_pp.rb
+++ b/test/test_pp.rb
@@ -184,6 +184,18 @@ class PPDelegateTest < Test::Unit::TestCase
def test_delegate
assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]")
end
+
+ def test_delegate_cycle
+ a = HasPrettyPrint.new nil
+
+ a.instance_eval {@a = a}
+ cycle_pretty_inspect = a.pretty_inspect
+
+ a.instance_eval {@a = SimpleDelegator.new(a)}
+ delegator_cycle_pretty_inspect = a.pretty_inspect
+
+ assert_equal(cycle_pretty_inspect, delegator_cycle_pretty_inspect)
+ end
end
class PPFileStatTest < Test::Unit::TestCase