Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

StringIO#each_line

(?::String sep, ?::Integer limit, ?chomp: ::boolish) { (::String) -> untyped } -> self
(?::String sep, ?::Integer limit, ?chomp: ::boolish) -> ::Enumerator[::String, self]
each_line(sep = $/, chomp: false) {|line| ... } → self
each_line(limit, chomp: false) {|line| ... } → self
each_line(sep, limit, chomp: false) {|line| ... } → self

Alias of #each

With a block given calls the block with each remaining line (see “Position” below) in the stream; returns self.

Leaves stream position at end-of-stream.

No Arguments

With no arguments given, reads lines using the default record separator (global variable $/, whose initial value is "\n").

Example
strio = StringIO.new(TEXT)
strio.each_line {|line| p line }
strio.eof? # => true

Output:

Example
"First line\n"
"Second line\n"
"\n"
"Fourth line\n"
"Fifth line\n"

Argument sep

With only string argument sep given, reads lines using that string as the record separator:

Example
strio = StringIO.new(TEXT)
strio.each_line(' ') {|line| p line }

Output:

Example
"First "
"line\nSecond "
"line\n\nFourth "
"line\nFifth "
"line\n"

Argument limit

With only integer argument limit given, reads lines using the default record separator; also limits the size (in characters) of each line to the given limit:

Example
strio = StringIO.new(TEXT)
strio.each_line(10) {|line| p line }

Output:

Example
"First line"
"\n"
"Second lin"
"e\n"
"\n"
"Fourth lin"
"e\n"
"Fifth line"
"\n"

Arguments sep and limit

With arguments sep and limit both given, honors both:

Example
strio = StringIO.new(TEXT)
strio.each_line(' ', 10) {|line| p line }

Output:

Example
"First "
"line\nSecon"
"d "
"line\n\nFour"
"th "
"line\nFifth"
" "
"line\n"

Position

As stated above, method each remaining line in the stream.

In the examples above each strio object starts with its position at beginning-of-stream; but in other cases the position may be anywhere (see StringIO#pos):

Example
strio = StringIO.new(TEXT)
strio.pos = 30 # Set stream position to character 30.
strio.each_line {|line| p line }

Output:

Example
" line\n"
"Fifth line\n"

In all the examples above, the stream position is at the beginning of a character; in other cases, that need not be so:

Example
s = 'こんにちは'  # Five 3-byte characters.
strio = StringIO.new(s)
strio.pos = 3   # At beginning of second character.
strio.each_line {|line| p line }
strio.pos = 4   # At second byte of second character.
strio.each_line {|line| p line }
strio.pos = 5   # At third byte of second character.
strio.each_line {|line| p line }

Output:

Example
"んにちは"
"\x82\x93にちは"
"\x93にちは"

Special Record Separators

Like some methods in class IO, StringIO.each honors two special record separators; see Special Line Separators.

Example
strio = StringIO.new(TEXT)
strio.each_line('') {|line| p line } # Read as paragraphs (separated by blank lines).

Output:

Example
"First line\nSecond line\n\n"
"Fourth line\nFifth line\n"
Example
strio = StringIO.new(TEXT)
strio.each_line(nil) {|line| p line } # "Slurp"; read it all.

Output:

Example
"First line\nSecond line\n\nFourth line\nFifth line\n"

Keyword Argument chomp

With keyword argument chomp given as true (the default is false), removes trailing newline (if any) from each line:

Example
strio = StringIO.new(TEXT)
strio.each_line(chomp: true) {|line| p line }

Output:

Example
"First line"
"Second line"
""
"Fourth line"
"Fifth line"

With no block given, returns a new Enumerator.

Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.

See also #each #each_codepoint #eof #each_char also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0