Method: Pathname#write_text

Defined in:
lib/pleasant_path/pathname.rb

#write_text(text) ⇒ self

Writes text to the file indicated by the Pathname, overwriting the file if it exists. Creates the file if it does not exist, including any necessary parent directories. Returns the Pathname.

Examples:

Dir.exist?("path")           # == false
Dir.exist?("path/to")        # == false
File.exist?("path/to/file")  # == false

Pathname.new("path/to/file").write_text("hello world")
  # == Pathname.new("path/to/file")

File.read("path/to/file")    # == "hello world"

Parameters:

Returns:

  • (self)


945
946
947
948
# File 'lib/pleasant_path/pathname.rb', line 945

def write_text(text)
  self.make_dirname.open("w"){|f| f.write(text) }
  self
end