Linefeed turns a chunked byte stream into individually yielded lines.
When you're downstream of the read on a binary-mode chunked stream and want a push-style
take on #each_line that doesn't burn too much memory.
gem install linefeedOr add it to your Gemfile:
gem "linefeed"Including Linefeed supplies two methods, #<< and #close. The idea is for external
producers to drive processing by calls to these methods.
-
#<<accepts an arbitrary-size chunk of incoming data and yields each LF-terminated line to a handler set bylinefeed { |line| ... }. Lines yielded will be 8-bit ASCII strings and include the trailing LF. -
#closemarks end-of-incoming-data; if any data persists in the buffer, this yields a final unterminated string to the same handler.
These method names are intentionally IO-ish so that you can mingle regular output files
& IO streams with linefeed objects.
require "linefeed"
class Collector
include Linefeed
def initialize
@lines = []
linefeed { |line| @lines << line }
end
end
collector = Collector.new
collector << "hello\nwor"
collector << "ld\n"
collector.close
# @lines => ["hello\n", "world\n"]Write custom #<< and #close handlers by passing blocks to super blocks:
def <<(chunk)
super(chunk) do |line|
puts escape(line)
end
end
def close
super do |line|
puts escape(line) + "\n"
end
puts " -- all done."
endSee Examples for more, like daisy-chaining, or updating a digest.
MIT license. Copyright (c) 2026 inopinatus
Visit https://github.com/inopinatus/linefeed to open a PR.