class IOStreams::Line::Writer

Attributes

delimiter[R]

Public Class Methods

new(output_stream, delimiter: $/, original_file_name: nil) click to toggle source

A delimited stream writer that will write to the supplied output stream.

The output stream will have the encoding of data written to it. To change the output encoding, use IOStreams::Encode::Writer.

Parameters

output_stream
  The output stream that implements #write

delimiter: [String]
  Add the specified delimiter after every record when writing it
  to the output stream
  Default: OS Specific. Linux: "\n"
Calls superclass method IOStreams::Writer::new
# File lib/io_streams/line/writer.rb, line 27
def initialize(output_stream, delimiter: $/, original_file_name: nil)
  super(output_stream)
  @delimiter = delimiter
end
stream(output_stream, **args) { |output_stream| ... } click to toggle source

Write a line at a time to a stream.

# File lib/io_streams/line/writer.rb, line 7
def self.stream(output_stream, **args)
  # Pass-through if already a line writer
  return yield(output_stream) if output_stream.is_a?(self.class)

  yield new(output_stream, **args)
end

Public Instance Methods

<<(data) click to toggle source

Write a line to the output stream

Example:

IOStreams.path('a.txt').writer(:line) do |stream|
  stream << 'first line' << 'second line'
end
# File lib/io_streams/line/writer.rb, line 38
def <<(data)
  write(data)
  self
end
write(data) click to toggle source

Write a line to the output stream followed by the delimiter. Returns [Integer] the number of bytes written.

Example:

IOStreams.path('a.txt').writer(:line) do |stream|
  count = stream.write('first line')
  puts "Wrote #{count} bytes to the output file, including the delimiter"
end
# File lib/io_streams/line/writer.rb, line 51
def write(data)
  output_stream.write(data.to_s + delimiter)
end