Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Pathname#root?

() -> bool
root? → true or false

Returns whether the path in self points to a root directory.

On a non-Windows system, a root directory path is one whose name begins with one or more slash characters (‘’/‘):

Example
Pathname('/').root?       # => true
Pathname('////').root?    # => true
Pathname('/usr').root?    # => false
Pathname('foo').root?     # => false

Does not resolve dot directories:

Example
Pathname('/usr/.').root?  # => false
Pathname('/usr/..').root? # => false

On a Windows system, a root directory path is one whose name begins as above, or with a device letter followed by a colon character (':') and one or more slash characters (‘’/‘):

Example
Pathname('/').root?      # => true
Pathname('////').root?   # => true
Pathname('C:/').root?    # => true
Pathname('C:////').root? # => true
Pathname('c:/').root?    # => true
Pathname('H:/').root?    # => true
Pathname('C:/m').root?   # => false
Pathname('C:').root?     # => false
See also #rmtree #setgid? #rmdir #setuid? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0