module ADSP::Stream::WriterHelpers

ADSP::Stream::WriterHelpers module.

Public Class Methods

included(klass) click to toggle source

Extends target klass with additional class methods.

# File lib/adsp/stream/writer_helpers.rb, line 97
def self.included(klass)
  klass.extend ClassMethods
end

Public Instance Methods

<<(object) click to toggle source

Writes object to stream.

# File lib/adsp/stream/writer_helpers.rb, line 13
def <<(object)
  write object
end
print(*objects, field_separator: $OUTPUT_FIELD_SEPARATOR, record_separator: $OUTPUT_RECORD_SEPARATOR) click to toggle source

Writes objects to stream. Uses field_separator for each object. Uses record_separator for group of objects.

printf(*args) click to toggle source

Formats each argument and writes to stream.

# File lib/adsp/stream/writer_helpers.rb, line 32
def printf(*args)
  write sprintf(*args)

  nil
end
putc(object, encoding: ::Encoding::BINARY) click to toggle source

Writes first char of object to stream. Numeric object uses encoding for providing first char.

# File lib/adsp/stream/writer_helpers.rb, line 40
def putc(object, encoding: ::Encoding::BINARY)
  case object
  when ::Numeric
    write object.chr(encoding)
  when ::String
    write object[0]
  else
    raise ValidateError, "invalid object: \"#{object}\" for putc"
  end

  object
end
puts(*objects) click to toggle source

Writes objects to stream.

# File lib/adsp/stream/writer_helpers.rb, line 54
def puts(*objects)
  objects.each do |object|
    if object.is_a? ::Array
      puts(*object)
      next
    end

    source  = object.to_s
    newline = "\n".encode source.encoding

    # Do not add newline if source ends with newline.
    if source.end_with? newline
      write source
    else
      write source + newline
    end
  end

  nil
end