» Ruby » MagicNumberType infers a data type from the data's leading bytes
Ruby HTML » Ruby »
MagicNumberType infers mime types from leading bits

Gem Version Build Status Code Climate Coverage Status


Magic numbers are the first bits of a file or data stream which uniquely identify the type of file or data stream.

For example when the first bits are "BM", this identifies the file as a bitmap image file.

For docs go to

Want to help? We're happy to get pull requests.


To install using a Gemfile, add this:

gem "sixarm_ruby_magic_number_type", ">= 2.0.4", "< 3"

To install using the command line, run this:

gem install sixarm_ruby_magic_number_type -v ">= 2.0.4, < 3"

To install using the command line with high security, run this:

gem cert --add sixarm.pem && gem sources --add
gem install sixarm_ruby_magic_number_type -v ">= 2.0.4, < 3" --trust-policy HighSecurity

To require the gem in your code:

require "sixarm_ruby_magic_number_type"


This gem infers based on widespread programming conventions for data file formats.

These magic numbers are by convention and we are using this guide:

Typical uses of magic numbers:

  • to quickly identify a file's data type
  • to check if data matches the file's MIME type or extension
  • to check if a web form file upload matches its HTTP content type