Class: IO

Inherits:
Object show all
Defined in:
lib/pleasant_path/io.rb

Instance Method Summary collapse

Instance Method Details

#read_lines(eol: $/) ⇒ Array<String>

Note:

Not to be confused with IO#readlines, which retains end-of-line characters.

Reads all lines from the IO, and returns them with eol (end-of-line) characters stripped.

Examples:

File.read("in.txt")          # == "one\ntwo\n"

File.open("in.txt") do |io|
  io.read_lines              # == ["one", "two"]
end                          # == ["one", "two"]

Parameters:

  • eol (String) (defaults to: $/)

Returns:



42
43
44
# File 'lib/pleasant_path/io.rb', line 42

def read_lines(eol: $/)
  self.readlines(eol, chomp: true)
end

#write_lines(lines, eol: $/) ⇒ lines

Writes each object in lines as a string plus eol (end-of-line) characters to the IO. Returns lines, unmodified.

Examples:

File.open("out.txt") do |io|
  io.write_lines([:one, :two])  # == [:one, :two]
end                             # == [:one, :two]

File.read("out.txt")            # == "one\ntwo\n"

Parameters:

Returns:

  • (lines)


18
19
20
21
22
23
24
25
# File 'lib/pleasant_path/io.rb', line 18

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