ruby-changes:58249
From: Ary <ko1@a...>
Date: Tue, 15 Oct 2019 13:25:23 +0900 (JST)
Subject: [ruby-changes:58249] 96617ad1d5 (master): IRB colorize: take into account recursive arrays and hashes (#2555)
https://git.ruby-lang.org/ruby.git/commit/?id=96617ad1d5 From 96617ad1d57a13e9a282fb663ea73e4801519389 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig <asterite@g...> Date: Tue, 15 Oct 2019 01:25:05 -0300 Subject: IRB colorize: take into account recursive arrays and hashes (#2555) [Bug #16250] diff --git a/lib/irb/color.rb b/lib/irb/color.rb index 6b48964..32de6cd 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -70,16 +70,30 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L70 $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 diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb index 3ecbee8..ba76164 100644 --- a/test/irb/test_color.rb +++ b/test/irb/test_color.rb @@ -125,6 +125,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L125 1 => true, 2.3 => true, ['foo', :bar] => true, + (a = []; a << a; a) => false, + (h = {}; h[h] = h; h) => false, { a: 4 } => true, /reg/ => true, (1..3) => true, -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/