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
pathname_builtin.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 2143
def extname() File.extname(@path) end
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.