Linefeed

Linefeed turns a chunked byte stream into individually yielded lines.

Why?

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.

Install

gem install linefeed

Or add it to your Gemfile:

gem "linefeed"

Protocol

Including Linefeed supplies two methods, #<< and #close. The idea is for external producers to drive processing by calls to these methods.

These method names are intentionally IO-ish so that you can mingle regular output files & IO streams with linefeed objects.

Usage

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."
end

See Examples for more, like daisy-chaining, or updating a digest.

License

MIT license. Copyright © 2026 inopinatus

Contributing

Visit github.com/inopinatus/linefeed to open a PR.