find the extension of a csv file in ruby

To find the extension of a csv file in Ruby, you can make use of the File.extname method which returns the extension (including the dot .) of a file path. Here's how you can use it:

main.rb
file_path = "path/to/your/file.csv"
extension = File.extname(file_path) # returns ".csv"

# To remove the dot from the extension, you can use slice method
extension_without_dot = extension.slice(1..-1) # returns "csv"
218 chars
6 lines

Alternatively, you can also use regular expressions to match the extension of the file path:

main.rb
file_path = "path/to/your/file.csv"
extension = file_path.match(/\.([^.]+)$/)[1] # returns "csv"
97 chars
3 lines

Both of these methods should give you the file extension of your CSV file in Ruby.

gistlibby LogSnag