From 74d6047fe2e72f6279f67889654fc2d1c21db097 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 28 Jul 2023 16:34:56 -0400 Subject: [PATCH] Remove colon from Hash key symbols --- lib/debug/variable_inspector.rb | 13 ++++++++++--- test/debug/variable_inspector_test.rb | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/debug/variable_inspector.rb b/lib/debug/variable_inspector.rb index f74e170db..0945e863f 100644 --- a/lib/debug/variable_inspector.rb +++ b/lib/debug/variable_inspector.rb @@ -18,7 +18,7 @@ def named_members_of(obj) return [] if NaiveString === obj members = case obj - when Hash then obj.map { |k, v| Variable.new(name: value_inspect(k), value: v) } + when Hash then obj.map { |k, v| Variable.new(name: inspect_hash_key(k), value: v) } when Struct then obj.members.map { |name| Variable.new(name: name, value: obj[name]) } when String members = [ @@ -54,6 +54,15 @@ def value_inspect(obj, short: true) self.class.value_inspect(obj, short: short) end + def inspect_hash_key(key) + # Special-case for symbols so debugger UIs render `a: 1` instead of two colons like `:a: 1` + return key.to_s if key.is_a?(Symbol) + + value_inspect(key) + end + + MAX_LENGTH = 180 + def self.value_inspect(obj, short: true) # TODO: max length should be configurable? str = LimitedPP.safe_inspect obj, short: short, max_length: MAX_LENGTH @@ -65,8 +74,6 @@ def self.value_inspect(obj, short: true) end end - MAX_LENGTH = 180 - # TODO: Replace with Reflection helpers once they are merged # https://github.com/ruby/debug/pull/1002 M_INSTANCE_VARIABLES = method(:instance_variables).unbind diff --git a/test/debug/variable_inspector_test.rb b/test/debug/variable_inspector_test.rb index 33c833685..bf4efed8f 100644 --- a/test/debug/variable_inspector_test.rb +++ b/test/debug/variable_inspector_test.rb @@ -48,7 +48,7 @@ def test_named_members_of_hash expected = [ Variable.internal(name: '#class', value: Hash), - Variable.new(name: ':sym', value: "has Symbol key"), + Variable.new(name: 'sym', value: "has Symbol key"), Variable.new(name: '"str"', value: "has String key"), Variable.new(name: '1', value: "has Integer key"), ]