Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file name and extension in Ruby

Tags:

file

ruby

People also ask

How do I get the file extension in Ruby?

As there are more than one way of getting things done in Ruby, File extension also can be obtained in more than one way. Use of File#extname is more suitable in Ruby get file extension from string.

What is the extension of Ruby file?

An RB file is a software program written in Ruby, an object-oriented scripting language. Ruby is designed to be simple, efficient, and easy to read. RB files can be edited with a text editor and run using Ruby.


You can use the following functions for your purpose:

path = "/path/to/xyz.mp4"

File.basename(path)         # => "xyz.mp4"
File.extname(path)          # => ".mp4"
File.basename(path, ".mp4") # => "xyz"
File.basename(path, ".*")   # => "xyz"
File.dirname(path)          # => "/path/to"