summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAry Borenszweig <asterite@gmail.com>2019-10-15 01:25:05 -0300
committerTakashi Kokubun <takashikkbn@gmail.com>2019-10-14 21:25:05 -0700
commit96617ad1d57a13e9a282fb663ea73e4801519389 (patch)
tree1b1f4404b5a2c1a88c3bba42b292e99ff7305ba1 /lib
parentddd42d8ebdefc520b0c7d6ada4fb0de6969a49f8 (diff)
IRB colorize: take into account recursive arrays and hashes (#2555)
[Bug #16250]
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/color.rb22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index 6b489646b5..32de6cdbea 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -70,16 +70,30 @@ module IRB # :nodoc:
$stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
end
- def inspect_colorable?(obj)
+ def inspect_colorable?(obj, seen = {})
case obj
when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
true
when Hash
- obj.all? { |k, v| inspect_colorable?(k) && inspect_colorable?(v) }
+ if seen.has_key?(obj.object_id)
+ false
+ else
+ seen[obj.object_id] = true
+ colorable = obj.all? { |k, v| inspect_colorable?(k, seen) && inspect_colorable?(v, seen) }
+ seen.delete(obj.object_id)
+ colorable
+ end
when Array
- obj.all? { |o| inspect_colorable?(o) }
+ if seen.has_key?(obj.object_id)
+ false
+ else
+ seen[obj.object_id] = true
+ colorable = obj.all? { |o| inspect_colorable?(o, seen) }
+ seen.delete(obj.object_id)
+ colorable
+ end
when Range
- inspect_colorable?(obj.begin) && inspect_colorable?(obj.end)
+ inspect_colorable?(obj.begin, seen) && inspect_colorable?(obj.end, seen)
when Module
!obj.name.nil?
else