Sha256: 2a81fc2e846d4ffc3aaa93adfa3e9e6b952005f33a296fa86ef13e5a77fb744c

Contents?: true

Size: 670 Bytes

Versions: 2

Compression:

Stored size: 670 Bytes

Contents

require 'stringio'

module Termistat
  #
  # TeeIO wraps a StringIO object such that writes to the IO can trigger events
  # immediately, but the entirety of the string is available later.
  #
  # initialize method takes a block that will be called on every write.
  #
  class TeeIO < StringIO
    # create a new TeeIO object
    #
    # == Example
    #  $stdout = TeeIO.new {|str| STDOUT.puts str.reverse }
    #
    def initialize(&block)
      @io = StringIO.new
      @callback = block
    end

    def write(chars)
      @io.write(chars)
      @callback.call(chars)
    end

    def string
      @io.string
    end

    def flush
      @io.flush
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
termistat-0.0.3 lib/termistat/tee_io.rb
termistat-0.0.2 lib/termistat/tee_io.rb