From 9c4db3281cb9621ffc5a3957ecea9ce78f9cc499 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Mon, 20 Nov 2017 18:02:43 +0000 Subject: [PATCH] Avoid infinite loops when calling inspect Following my earlier changes, some slightly buggy code in one of my applications triggered this. The problem went away once the code was corrected but it is obviously worth avoiding this to start with. --- lib/her/model/introspection.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/her/model/introspection.rb b/lib/her/model/introspection.rb index 348a013a..f9f4dd7e 100644 --- a/lib/her/model/introspection.rb +++ b/lib/her/model/introspection.rb @@ -12,6 +12,9 @@ module Introspection # @user = User.find(1) # p @user # => # def inspect + first = Thread.current[:her_inspect_objects].nil? + Thread.current[:her_inspect_objects] = [] if first + resource_path = begin request_path rescue Her::Errors::PathError => e @@ -19,7 +22,16 @@ def inspect "" end - "#<#{self.class}(#{resource_path}) #{attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(" ")}>" + result = "#<#{self.class}(#{resource_path}) " + + if Thread.current[:her_inspect_objects].include?(self) + result << '...>' + else + Thread.current[:her_inspect_objects] << self + result << attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(' ') + '>' + end + ensure + Thread.current[:her_inspect_objects] = nil if first end private