Sha256: 150865ad5dbcca625c8f5f59be8ad28a13dcc8482ba234cac4dda14a351906c9

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class IO

  # Writes each object in +lines+ as a string plus end-of-line (EOL)
  # 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 [Enumerable<#to_s>]
  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 all end-of-line
  # (EOL) characters stripped.
  #
  # @note Not to be confused with +IO#readlines+, which retains
  #   end-of-line (EOL) 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-1.3.0 lib/pleasant_path/io.rb