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#extname

() -> ::String
extname → extension

Returns the filename extension of self – usually the portion of the string path beginning from the last period:

Example
Pathname('t.rb').extname               # => ".rb"
Pathname('foo.bar.t.rb').extname       # => ".rb"
Pathname('foo/bar/t.rb').extname       # => ".rb"
Pathname('nosuch.txt').extname         # => ".txt"  # Path need not exist.

Returns the entire string when there is no period:

Example
Pathname('foo').extname # => ""

Returns an empty string when the only period is the first character:

Example
Pathname('.irbrc').extname # => ""

Returns an empty string or '.' when path ends with a period:

Example
Pathname('foo.').extname    # => ""   # On Windows.
Pathname('foo.').extname    # => "."  # Elsewhere.
Pathname('foo....').extname # => ""   # On Windows.
Pathname('foo....').extname # => "."  # Elsewhere.
See also #expand_path #file? #exist? #find also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0