1
- With a block given, forms the substrings (" lines" )
1
+ With a block given, forms the substrings (lines)
2
2
that are the result of splitting +self+
3
- at each occurrence of the given line separator +line_sep +;
3
+ at each occurrence of the given +record_separator +;
4
4
passes each line to the block;
5
- returns +self+:
5
+ returns +self+.
6
6
7
+ With the default +record_separator+:
8
+
9
+ $/ # => "\n"
7
10
s = <<~EOT
8
11
This is the first line.
9
12
This is line two.
10
13
11
14
This is line four.
12
15
This is line five.
13
16
EOT
14
-
15
17
s.each_line {|line| p line }
16
18
17
19
Output:
@@ -22,9 +24,10 @@ Output:
22
24
"This is line four.\n"
23
25
"This is line five.\n"
24
26
25
- With a different +line_sep +:
27
+ With a different +record_separator +:
26
28
27
- s.each_line(' is ') {|line| p line }
29
+ record_separator = ' is '
30
+ s.each_line(record_separator) {|line| p line }
28
31
29
32
Output:
30
33
@@ -34,7 +37,7 @@ Output:
34
37
"line four.\nThis is "
35
38
"line five.\n"
36
39
37
- With +chomp+ as +true+, removes the trailing +line_sep + from each line:
40
+ With +chomp+ as +true+, removes the trailing +record_separator + from each line:
38
41
39
42
s.each_line(chomp: true) {|line| p line }
40
43
@@ -46,15 +49,18 @@ Output:
46
49
"This is line four."
47
50
"This is line five."
48
51
49
- With an empty string as +line_sep +,
52
+ With an empty string as +record_separator +,
50
53
forms and passes "paragraphs" by splitting at each occurrence
51
54
of two or more newlines:
52
55
53
- s.each_line('') {|line| p line }
56
+ record_separator = ''
57
+ s.each_line(record_separator) {|line| p line }
54
58
55
59
Output:
56
60
57
61
"This is the first line.\nThis is line two.\n\n"
58
62
"This is line four.\nThis is line five.\n"
59
63
60
64
With no block given, returns an enumerator.
65
+
66
+ Related: see {Iterating}[rdoc-ref:String@Iterating].
0 commit comments