Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

IO#each_line

ios.each_line(sep=$/ [, getline_args]) {|line| block } → ios
ios.each_line(limit [, getline_args]) {|line| block } → ios
ios.each_line(sep, limit [, getline_args]) {|line| block } → ios
ios.each_line(...) → an_enumerator

Alias of #each

Executes the block for every line in ios, where lines are separated by sep. ios must be opened for reading or an IOError will be raised.

If no block is given, an enumerator is returned instead.

Example
f = File.new("testfile")
f.each {|line| puts "#{f.lineno}: #{line}" }

produces:

1: This is line one
2: This is line two
3: This is line three
4: And so on...

See IO.readlines for details about getline_args.

See also #each #each_codepoint #echo= #each_char also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1