Method: Pathname#edit_text

Defined in:
lib/pleasant_path/pathname.rb

#edit_text {|text| ... } ⇒ self

Reads the file indicated by the Pathname, and yields the entire contents as a String to the given block for editing. Writes the return value of the block back to the file, overwriting previous contents. Returns the Pathname.

Examples:

Update JSON data file

File.read("data.json")  # == '{"nested":{"key":"value"}}'

Pathname.new("data.json").edit_text do |text|
  data = JSON.parse(text)
  data["nested"]["key"] = "new value"
  data.to_json
end                     # == '{"nested":{"key":"new value"}}'

File.read("data.json")  # == '{"nested":{"key":"new value"}}'

Yields:

  • (text)

Yield Parameters:

Yield Returns:

Returns:

  • (self)

See Also:



1059
1060
1061
1062
# File 'lib/pleasant_path/pathname.rb', line 1059

def edit_text(&block)
  File.edit_text(self, &block)
  self
end