Method: Pathname#edit_lines

Defined in:
lib/pleasant_path/pathname.rb

#edit_lines(eol: $/) {|lines| ... } ⇒ self

Reads the file indicated by the Pathname, and yields the entire contents as an Array of lines to the given block for editing. Writes the return value of the block back to the file, overwriting previous contents. eol (end-of-line) characters are stripped from each line when reading, and appended to each line when writing. Returns the Pathname.

Examples:

Dedup lines of file

File.read("entries.txt")  # == "AAA\nBBB\nBBB\nCCC\nAAA\n"

Pathname.new("entries.txt").edit_lines(&:uniq)
  # == ["AAA", "BBB", "CCC"]

File.read("entries.txt")  # == "AAA\nBBB\nCCC\n"

Parameters:

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

Yields:

  • (lines)

Yield Parameters:

Yield Returns:

Returns:

  • (self)

See Also:



1086
1087
1088
1089
# File 'lib/pleasant_path/pathname.rb', line 1086

def edit_lines(eol: $/, &block)
  File.edit_lines(self, eol: eol, &block)
  self
end