From fe5729692863ffb48362e2da2fc49eb8929b4131 Mon Sep 17 00:00:00 2001 From: Alexis Bernard Date: Mon, 30 Sep 2024 15:16:35 +0200 Subject: [PATCH] Add method RDoc::ClassModule#super_classes The purpose is to list all class ancestors. --- lib/rdoc/code_object/class_module.rb | 14 ++++++++++++++ test/rdoc/test_rdoc_class_module.rb | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/lib/rdoc/code_object/class_module.rb b/lib/rdoc/code_object/class_module.rb index c69e14b5e4..589433de5d 100644 --- a/lib/rdoc/code_object/class_module.rb +++ b/lib/rdoc/code_object/class_module.rb @@ -710,6 +710,20 @@ def superclass=(superclass) @superclass = superclass end + ## + # Get all super classes of this class in an array. The last element might be + # a string if the name is unknown. + + def super_classes + result = [] + parent = self + while parent = parent.superclass + result << parent + return result if parent.is_a?(String) + end + result + end + def to_s # :nodoc: if is_alias_for then "#{self.class.name} #{self.full_name} -> #{is_alias_for}" diff --git a/test/rdoc/test_rdoc_class_module.rb b/test/rdoc/test_rdoc_class_module.rb index 27d37cb7dd..87311a6ce8 100644 --- a/test/rdoc/test_rdoc_class_module.rb +++ b/test/rdoc/test_rdoc_class_module.rb @@ -1279,6 +1279,12 @@ def test_superclass assert_equal @c3_h1, @c3_h2.superclass end + def test_super_classes + rdoc_c3_h1 = @xref_data.find_module_named('C3::H1') + rdoc_object = @xref_data.find_module_named('Object') + assert_equal [rdoc_c3_h1, rdoc_object, "BasicObject"], @c3_h2.super_classes + end + def test_update_aliases_class n1 = @xref_data.add_module RDoc::NormalClass, 'N1' n1_k2 = n1.add_module RDoc::NormalClass, 'N2'