Search code examples
rubymime-types

How to determine mime type by the file extension? ( Ruby )


I've seen questions like this on stackoverflow but nothing was for ruby.

So I have file extensions like these:

css, html, js, json

And I want to convert them into:

text/css, text/html, text/javascript, application/json

I want to achieve this in ruby.

So after writing this:

ext_to_mime("css")

I should get:

text/css

If there are any gems for this they are also welcome.


Solution

  • You could use the mime-types gem:

    puts MIME::Types.type_for('css')
      => [text/css]