Class: String

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

Instance Method Summary collapse

Instance Method Details

#/(child) ⇒ Pathname

Constructs a Pathname from the String, and appends child to the Pathname.

Examples:

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

Parameters:

Returns:



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

def /(child)
  self.path / child
end

#^(sibling) ⇒ Pathname

Deprecated.

Use Pathname#^.

Constructs a Pathname from the String, and appends sibling to the dirname of the Pathname.

The mnemonic for this operator is that the result is formed by going up one directory level from the original path, then going back down to sibling.

Examples:

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

Parameters:

Returns:

See Also:



46
47
48
# File 'lib/pleasant_path/string.rb', line 46

def ^(sibling)
  self.path ^ sibling
end

#append_to_file(file) ⇒ self

Appends the String to the specified file. Creates the file if it does not exist, including any necessary parent directories. Returns the String.

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"

Parameters:

Returns:

  • (self)

See Also:



96
97
98
99
# File 'lib/pleasant_path/string.rb', line 96

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

#globArray<Pathname>

Deprecated.

Use Pathname.glob.

Returns an array of Pathnames which match the filename pattern contained in the String.

Examples:

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

Returns:

See Also:



61
62
63
# File 'lib/pleasant_path/string.rb', line 61

def glob
  Pathname.glob(self)
end

#to_pathnamePathname Also known as: path

Constructs a Pathname from the String.

Examples:

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

Returns:



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

def to_pathname
  Pathname.new(self)
end

#write_to_file(file) ⇒ self

Writes the String 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 String.

Examples:

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

Parameters:

Returns:

  • (self)

See Also:



77
78
79
80
# File 'lib/pleasant_path/string.rb', line 77

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