Jon
Jon

Reputation: 179

Exclude the "." from a file extension in Rails

How do I the dot when getting the file extension name in rails?

Right now I'm using File.extname but it's keeping the "." before giving me the name

Upvotes: 4

Views: 3503

Answers (1)

Charles Caldwell
Charles Caldwell

Reputation: 17169

Would this work for you?

File.extname('something.jpg').delete('.') #=> 'jpg'

You could even wrap it up in a helper:

def file_extension(filename)
  File.extname(filename).delete('.')
end

file_extension('something.jpg') #=> 'jpg'

Upvotes: 14

Related Questions