Skip to content

Commit 4bb3a63

Browse files
vinistockst0012
andcommitted
Define Heading conditionally as a struct
Rubies older than v4.0.0 have their documentation data serialized using a struct, so if we change it to a class it fails. To maintain compatibility, we define `Heading` conditionally as a struct on older rubies. Co-authored-by: Stan Lo <st0012@users.noreply.github.com>
1 parent 0602d13 commit 4bb3a63

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/rdoc/markup/heading.rb

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22

33
module RDoc
44
class Markup
5+
# IMPORTANT! This weird workaround is required to ensure that RDoc can correctly deserializing Marshal data from
6+
# older rubies. Older rubies have `Heading` as a struct, so if we change it to a class, deserialization fails
7+
if RUBY_VERSION.start_with?("4.")
8+
class Heading < Element
9+
#: String
10+
attr_reader :text
11+
12+
#: Integer
13+
attr_accessor :level
14+
15+
#: (Integer, String) -> void
16+
def initialize(level, text)
17+
super()
18+
19+
@level = level
20+
@text = text
21+
end
22+
23+
#: (Object) -> bool
24+
def ==(other)
25+
other.is_a?(Heading) && other.level == @level && other.text == @text
26+
end
27+
end
28+
else
29+
Heading = Struct.new(:level, :text)
30+
end
31+
532
# A heading with a level (1-6) and text
633
#
734
# RDoc syntax:
@@ -13,13 +40,8 @@ class Markup
1340
# # Heading 1
1441
# ## Heading 2
1542
# ### Heading 3
16-
class Heading < Element
17-
#: String
18-
attr_reader :text
19-
20-
#: Integer
21-
attr_accessor :level
22-
43+
#
44+
class Heading
2345
# A singleton RDoc::Markup::ToLabel formatter for headings.
2446
#: () -> RDoc::Markup::ToLabel
2547
def self.to_label
@@ -43,19 +65,6 @@ def to_html.handle_regexp_CROSSREF(target)
4365
end
4466
end
4567

46-
#: (Integer, String) -> void
47-
def initialize(level, text)
48-
super()
49-
50-
@level = level
51-
@text = text
52-
end
53-
54-
#: (Object) -> bool
55-
def ==(other)
56-
other.is_a?(Heading) && other.level == @level && other.text == @text
57-
end
58-
5968
# @override
6069
#: (untyped) -> void
6170
def accept(visitor)

0 commit comments

Comments
 (0)