Sha256: e5e3b18121da666a0ed9a6bdae99c278808617afad1e39a346383344596e97b8

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

class String

  # Converts the string to a +Pathname+ object.
  #
  # @return [Pathname]
  def to_pathname
    Pathname.new(self)
  end

  # Alias of +String#to_pathname+.
  #
  # @return [Pathname]
  alias :path :to_pathname

  # Joins the string and the argument with a directory separator (a la
  # +File.join+) and returns the result as a +Pathname+ object.
  #
  # @example
  #   ("path/to" / "file").to_s #=> "path/to/file"
  #
  # @param child [String]
  # @return [Pathname]
  def /(child)
    self.path / child
  end

  # 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 mnenomic 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#^+.
  #
  # @example
  #   ("path/to/file1" ^ "file2").to_s #=> "path/to/file2"
  #
  # @param sibling [Pathname, String]
  # @return [Pathname]
  def ^(sibling)
    self.path ^ sibling
  end

  # Treats the string as a filename pattern, and expands the pattern
  # into matching paths as +Pathname+ objects.  See also +Dir.glob+ and
  # +Pathname.glob+.
  #
  # @return [Array<Pathname>]
  def glob
    Pathname.glob(self)
  end

  # 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.
  #
  # @param file [String, Pathname]
  # @return [String]
  def write_to_file(file)
    file.to_pathname.write_text(self)
    self
  end

  # 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.
  #
  # @param file [String, Pathname]
  # @return [String]
  def append_to_file(file)
    file.to_pathname.append_text(self)
    self
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pleasant_path-1.0.0 lib/pleasant_path/string.rb