Method: Path#dirname

Defined in:
lib/rubypath/path_operations.rb

#dirnamePath Also known as: parent

Return path to parent directory. If path is already an absolute or relative root nil will be returned.

Examples:

Get parent directory:

Path.new('/path/to/file').dir.path
#=> '/path/to'

Try to get parent of absolute root:

Path.new('/').dir
#=> nil

Try to get parent of relative root:

Path.new('.').dir
#=> nil

Returns:

  • (Path)

    Parent path or nil if path already points to an absolute or relative root.



167
168
169
170
171
172
# File 'lib/rubypath/path_operations.rb', line 167

def dirname
  return nil if %w[. /].include? internal_path

  dir = ::File.dirname internal_path
  dir.empty? ? nil : self.class.new(dir)
end