find the extension of a file in ruby

To find the extension of a file in Ruby, you can use the File.extname method. This method takes a file name as an argument and returns the extension of the file.

Here's an example:

main.rb
filename = "example.txt"
extension = File.extname(filename)
puts extension # Output: ".txt"
92 chars
4 lines

In this example, the File.extname method is used to find the extension of the file example.txt. The resulting extension (".txt") is then printed to the console using puts.

Note that the File.extname method only returns the file extension (including the period), not the entire file name. If you need the full file name without the extension, you can use the File.basename method.

related categories

gistlibby LogSnag