Module: Enumerable

Defined in:
lib/pleasant_path/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#append_to_file(file, eol: $/) ⇒ self

Appends each object in the Enumerable as a string plus eol (end-of-line) characters to the specified file. Creates the file if it does not exist, including any necessary parent directories. Returns the Enumerable.

Examples:

[:one, :two].append_to_file("out.txt")     # == [:one, :two]
File.read("out.txt")                       # == "one\ntwo\n"
[:three, :four].append_to_file("out.txt")  # == [:three, :four]
File.read("out.txt")                       # == "one\ntwo\nthree\nfour\n"

Parameters:

Returns:

  • (self)

See Also:



38
39
40
41
# File 'lib/pleasant_path/enumerable.rb', line 38

def append_to_file(file, eol: $/)
  file.to_pathname.append_lines(self, eol: eol)
  self
end

#write_to_file(file, eol: $/) ⇒ self

Writes each object in the Enumerable as a string plus eol (end-of-line) characters to the specified file, overwriting the file if it exists. Creates the file if it does not exist, including any necessary parent directories. Returns the Enumerable.

Examples:

[:one, :two].write_to_file("out.txt")  # == [:one, :two]
File.read("out.txt")                   # == "one\ntwo\n"

Parameters:

Returns:

  • (self)

See Also:



17
18
19
20
# File 'lib/pleasant_path/enumerable.rb', line 17

def write_to_file(file, eol: $/)
  file.to_pathname.write_lines(self, eol: eol)
  self
end