Class: String

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

Instance Method Summary collapse

Instance Method Details

#/(child) ⇒ Pathname

Joins the string and the argument with a directory separator (a la File.join) and returns the result as a Pathname object.

Examples:

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


26
27
28
# File 'lib/pleasant_path/string.rb', line 26

def /(child)
  self.path / child
end

#^(sibling) ⇒ Pathname

Treating the string as a path, joins the parent (dirname) of the path with the argument, and returns the result as a Pathname object. The mnemonic for this operator is that the resultant path goes up one directory level from the original, then goes down to the directory specified by the argument. See also Pathname#^.

Examples:

"path/to/file1" ^ "file2"  # == Pathname.new("path/to/file2")


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

def ^(sibling)
  self.path ^ sibling
end

#append_to_file(file) ⇒ String

Appends the string to the given file, and returns the string. The file is created if it does not exist. Any necessary parent directories are created if they do not exist.

Examples:

"hello".append_to_file("out.txt")   # == "hello"
File.read("out.txt")                # == "hello"
" world".append_to_file("out.txt")  # == " world"
File.read("out.txt")                # == "hello world"


84
85
86
87
# File 'lib/pleasant_path/string.rb', line 84

def append_to_file(file)
  file.to_pathname.append_text(self)
  self
end

#globArray<Pathname>

Treats the string as a filename pattern, and expands the pattern into matching paths as Pathname objects. See also Dir.glob and Pathname.glob.

Examples:

"*.txt".glob  # == Pathname.glob("*.txt")


53
54
55
# File 'lib/pleasant_path/string.rb', line 53

def glob
  Pathname.glob(self)
end

#to_pathnamePathname Also known as: path

Converts the string to a Pathname object.

Examples:

"path/to/file".to_pathname  # == Pathname.new("path/to/file")


9
10
11
# File 'lib/pleasant_path/string.rb', line 9

def to_pathname
  Pathname.new(self)
end

#write_to_file(file) ⇒ String

Writes the string to the given file, and returns the string. The file is overwritten if it already exists. Any necessary parent directories are created if they do not exist.

Examples:

"hello world".write_to_file("out.txt")  # == "hello world"
File.read("out.txt")                    # == "hello world"


67
68
69
70
# File 'lib/pleasant_path/string.rb', line 67

def write_to_file(file)
  file.to_pathname.write_text(self)
  self
end