Given the variable content_type = "application/pdf" that can also include any other mime type.
How can I get the default extension for the content type?
I have currently two solutions, which seem very “complicated”.
Hack the string
content_type.split("/")[1]
Use MIME::Types
require 'mime/types'
MIME::Types[content_type].first.extensions.first
Is there a better solution?
Your second solution with the mime type is the solution, which you should choose. There are several reasons for that:
Hack the stringcould be inconsistent or return unexpected results (think aboutapplication/postscripthas the extensioneps!)