Sha256: 00f31ba7076fc14e7f2909acc65a0e529ff060c2f14a60ee8d6a80756db21e8f

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module IRuby
  # IO-like object that publishes to 0MQ socket.
  class OStream
    attr_accessor :sync

    def initialize(session, name)
      @session, @name = session, name
    end

    def close
      @session = nil
    end

    def flush
    end

    def isatty
      false
    end
    alias_method :tty?, :isatty

    def read(*args)
      raise IOError, 'not opened for reading'
    end
    alias_method :next, :read
    alias_method :readline, :read

    def write(*obj)
      str = build_string { |sio| sio.write(*obj) }
      session_send(str)
    end
    alias_method :<<, :write
    alias_method :print, :write

    def printf(format, *obj)
      str = build_string { |sio| sio.printf(format, *obj) }
      session_send(str)
    end

    def puts(*obj)
      str = build_string { |sio| sio.puts(*obj) }
      session_send(str)
    end

    def writelines(lines)
      lines.each { |s| write(s) }
    end

    private

    def build_string
      StringIO.open { |sio| yield(sio); sio.string }
    end

    def session_send(str)
      raise 'I/O operation on closed file' unless @session

      @session.send(:publish, :stream, name: @name, text: str)
      nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iruby-0.5.0 lib/iruby/ostream.rb