Module: ADSP::Stream::WriterHelpers

Included in:
Writer
Defined in:
lib/adsp/stream/writer_helpers.rb

Overview

ADSP::Stream::WriterHelpers module.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Extends target klass with additional class methods.



97
98
99
# File 'lib/adsp/stream/writer_helpers.rb', line 97

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#<<(object) ⇒ Object

Writes object to stream.



13
14
15
# File 'lib/adsp/stream/writer_helpers.rb', line 13

def <<(object)
  write object
end

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/adsp/stream/writer_helpers.rb', line 20

def print(*objects, field_separator: $OUTPUT_FIELD_SEPARATOR, record_separator: $OUTPUT_RECORD_SEPARATOR)
  objects.each do |object|
    write object
    write field_separator unless field_separator.nil?
  end

  write record_separator unless record_separator.nil?

  nil
end

#printf(*args) ⇒ Object

Formats each argument and writes to stream.



32
33
34
35
36
# File 'lib/adsp/stream/writer_helpers.rb', line 32

def printf(*args)
  write sprintf(*args)

  nil
end

#putc(object, encoding: ::Encoding::BINARY) ⇒ Object

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



40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

Writes objects to stream.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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