Skip to content

Commit c129b61

Browse files
[DOC] Use RDoc inclusions in string.c (#5683)
As @peterzhu2118 and @duerst have pointed out, putting string method's RDoc into doc/ (which allows non-ASCII in examples) makes the "click to toggle source" feature not work for that method. This PR moves the primary method doc back into string.c, then includes RDoc from doc/string/*.rdoc, and also removes doc/string.rdoc. The affected methods are: ::new #bytes #each_byte #each_line #split The call-seq is in string.c because it works there; it did not work when the call-seq is in doc/string/*.rdoc. This PR also updates the relevant guidance in doc/documentation_guide.rdoc.
1 parent 1fd1f7b commit c129b61

File tree

8 files changed

+261
-274
lines changed

8 files changed

+261
-274
lines changed

doc/documentation_guide.rdoc

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,25 @@ involving new files <tt>doc/*.rdoc</tt>:
6060
# Documentation for module Bar goes here.
6161
module Bar; end
6262

63-
- For an instance method Baz#bat (defined in file <tt>baz.c</tt>),
64-
create file <tt>doc/baz.rdoc</tt>, declare class +Baz+
65-
and instance method +bat+, and place the method documentation above
66-
the method declaration:
63+
- For a method, things are different.
64+
Documenting a method as above disables the "click to toggle source" feature
65+
in the rendered documentaion.
6766

68-
# :markup: ruby
69-
class Baz
70-
# Documentation for method bat goes here.
71-
# (Don't forget the call-seq.)
72-
def bat; end
73-
end
74-
75-
- For a singleton method Bam.bah (defined in file <tt>bam.c</tt>),
76-
create file <tt>doc/bam.rdoc</tt>, declare class +Bam+
77-
and singleton method +bah+, and place the method documentation above
78-
the method declaration:
67+
Therefore it's best to use file inclusion:
7968

80-
# :markup: ruby
81-
class Bam
82-
# Documentation for method bah goes here.
83-
# (Don't forget the call-seq.)
84-
def self.bah; end
85-
end
69+
- Retain the call-seq in the C code.
70+
- Use file inclusion (+:include:+) to include text from an .rdoc file.
8671

87-
See these examples:
72+
Example:
8873

89-
- https://raw.githubusercontent.com/ruby/ruby/master/doc/string.rdoc
90-
- https://raw.githubusercontent.com/ruby/ruby/master/doc/transcode.rdoc
74+
/*
75+
* call-seq:
76+
* each_byte {|byte| ... } -> self
77+
* each_byte -> enumerator
78+
*
79+
* \:include: doc/string/each_byte.rdoc
80+
*
81+
*/
9182

9283
=== \RDoc
9384

doc/string.rdoc

Lines changed: 0 additions & 245 deletions
This file was deleted.

doc/string/bytes.rdoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Returns an array of the bytes in +self+:
2+
3+
'hello'.bytes # => [104, 101, 108, 108, 111]
4+
'тест'.bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
5+
'こんにちは'.bytes
6+
# => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]

doc/string/each_byte.rdoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Calls the given block with each successive byte from +self+;
2+
returns +self+:
3+
4+
'hello'.each_byte {|byte| print byte, ' ' }
5+
print "\n"
6+
'тест'.each_byte {|byte| print byte, ' ' }
7+
print "\n"
8+
'こんにちは'.each_byte {|byte| print byte, ' ' }
9+
print "\n"
10+
11+
Output:
12+
13+
104 101 108 108 111
14+
209 130 208 181 209 129 209 130
15+
227 129 147 227 130 147 227 129 171 227 129 161 227 129 175
16+
17+
Returns an enumerator if no block is given.

doc/string/each_line.rdoc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
With a block given, forms the substrings ("lines")
2+
that are the result of splitting +self+
3+
at each occurrence of the given line separator +line_sep+;
4+
passes each line to the block;
5+
returns +self+:
6+
7+
s = <<~EOT
8+
This is the first line.
9+
This is line two.
10+
11+
This is line four.
12+
This is line five.
13+
EOT
14+
15+
s.each_line {|line| p line }
16+
17+
Output:
18+
19+
"This is the first line.\n"
20+
"This is line two.\n"
21+
"\n"
22+
"This is line four.\n"
23+
"This is line five.\n"
24+
25+
With a different +line_sep+:
26+
27+
s.each_line(' is ') {|line| p line }
28+
29+
Output:
30+
31+
"This is "
32+
"the first line.\nThis is "
33+
"line two.\n\nThis is "
34+
"line four.\nThis is "
35+
"line five.\n"
36+
37+
With +chomp+ as +true+, removes the trailing +line_sep+ from each line:
38+
39+
s.each_line(chomp: true) {|line| p line }
40+
41+
Output:
42+
43+
"This is the first line."
44+
"This is line two."
45+
""
46+
"This is line four."
47+
"This is line five."
48+
49+
With an empty string as +line_sep+,
50+
forms and passes "paragraphs" by splitting at each occurrence
51+
of two or more newlines:
52+
53+
s.each_line('') {|line| p line }
54+
55+
Output:
56+
57+
"This is the first line.\nThis is line two.\n\n"
58+
"This is line four.\nThis is line five.\n"
59+
60+
With no block given, returns an enumerator.

0 commit comments

Comments
 (0)