Skip to content
class method

File.basename

(::string | ::_ToPath file_name, ?::string suffix) -> ::String
File.basename(file_name [, suffix] ) → base_name

Returns the last component of the filename given in file_name (after first stripping trailing separators), which can be formed using both File::SEPARATOR and File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is not nil. If suffix is given and present at the end of file_name, it is removed. If suffix is “.*”, any extension will be removed.

Example
File.basename("/home/gumby/work/ruby.rb")          #=> "ruby.rb"
File.basename("/home/gumby/work/ruby.rb", ".rb")   #=> "ruby"
File.basename("/home/gumby/work/ruby.rb", ".*")    #=> "ruby"
See also .atime .birthtime .absolute_path? .blockdev? also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0