class MemoryIO::IO

Main class to use {MemoryIO}.

def initialize(stream)

Parameters:
  • stream (#pos, #pos=, #read, #write) --
def initialize(stream)
  @stream = stream
end

def read(num_elements, from: nil, as: nil, force_array: false)

Other tags:
    See: Types -

Other tags:
    Note: -

Returns:
  • (String, Object, Array) -
    Parameters:
    • force_array (Boolean) --
    • as (nil, Symbol, Proc) --
    • from (Integer?) --
    • num_elements (Integer) --
    def read(num_elements, from: nil, as: nil, force_array: false)
      stream.pos = from if from
      return stream.read(num_elements) if as.nil?
      conv = to_proc(as, :read)
      # TODO: handle eof
      ret = Array.new(num_elements) { conv.call(stream) }
      ret = ret.first if num_elements == 1 && !force_array
      ret
    end

    def rewind

    Returns:
    • (0) -
    def rewind
      stream.pos = 0
    end

    def to_proc(as, rw)

    Other tags:
      Api: - private
    def to_proc(as, rw)
      ret = as.respond_to?(rw) ? as.method(rw) : as
      ret = ret.respond_to?(:call) ? ret : MemoryIO::Types.get_proc(ret, rw)
      raise ArgumentError, <<-EOERR.strip unless ret.respond_to?(:call)
    lid argument `as`: #{as.inspect}. It should be either a Proc or a supported type of MemoryIO::Types.
      EOERR
      ret
    end

    def write(objects, from: nil, as: nil)

    Other tags:
      See: Types -

    Returns:
    • (void) -

    Parameters:
    • as (nil, Symbol, Proc) --
    • from (Integer) --
    • objects (Object, Array) --
      def write(objects, from: nil, as: nil)
        stream.pos = from if from
        as ||= objects.class if objects.class.ancestors.include?(MemoryIO::Types::Type)
        return stream.write(objects) if as.nil?
        conv = to_proc(as, :write)
        Array(objects).map { |o| conv.call(stream, o) }
      end