Sha256: 7f00f094ac27ead92f64a3525b7bc1724de6faa8ae1ac8efb143e406616c8549

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

class IO

  # Writes each object in +lines+ as a string plus +eol+ (end-of-line)
  # characters to the IO.  Returns +lines+, unmodified.
  #
  # @example
  #   File.open("out.txt") do |io|
  #     io.write_lines([:one, :two])  # == [:one, :two]
  #   end                             # == [:one, :two]
  #
  #   File.read("out.txt")            # == "one\ntwo\n"
  #
  # @param lines [Enumerable<#to_s>]
  # @param eol [String]
  # @return [lines]
  def write_lines(lines, eol: $/)
    lines.each do |line|
      self.write(line)
      self.write(eol)
    end
    self.write("") # write something even if no lines
    lines
  end

  # Reads all lines from the IO, and returns them with +eol+
  # (end-of-line) characters stripped.
  #
  # @note Not to be confused with +IO#readlines+, which retains
  #   end-of-line characters.
  #
  # @example
  #   File.read("in.txt")          # == "one\ntwo\n"
  #
  #   File.open("in.txt") do |io|
  #     io.read_lines              # == ["one", "two"]
  #   end                          # == ["one", "two"]
  #
  # @param eol [String]
  # @return [Array<String>]
  def read_lines(eol: $/)
    self.readlines(eol, chomp: true)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pleasant_path-2.0.0 lib/pleasant_path/io.rb