Class: IO

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

Instance Method Summary collapse

Instance Method Details

#read_linesArray<String>

Reads from the IO all lines, and returns them as an array, end-of-line characters excluded. The $/ global string specifies what end-of-line characters to exclude.

(Not to be confused with IO#readlines which retains end-of-line characters in every string it returns.)

Examples:

# NOTE File inherits from IO
File.read("in.txt")            # == "one\ntwo\n"

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

Returns:



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

def read_lines
  self.readlines.each(&:chomp!)
end

#write_lines(lines) ⇒ Enumerable<#to_s>

Writes each object as a string plus a succeeding new line character ($/) to the IO. Returns the objects unmodified.

Examples:

# NOTE File inherits from IO
File.open("out.txt") do |file|
  file.write_lines([:one, :two])  # == [:one, :two]
end                               # == [:one, :two]

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

Parameters:

  • lines (Enumerable<#to_s>)

Returns:

  • (Enumerable<#to_s>)


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

def write_lines(lines)
  lines.each do |line|
    self.write(line)
    self.write($/)
  end
  self.write("") # write something even if no lines
  lines
end