The ruby-filemagic gem will do it:
require 'filemagic'
puts FileMagic.new(FileMagic::MAGIC_MIME).file(__FILE__)
# => text/x-ruby; charset=us-ascii
This gem does not look at the file extension at all. It reads a bit of the file contents and uses that to guess the file’s type.
More Related Contents:
- Boolean Validation
- Format a string containing Phone numbers
- How to understand nil vs. empty vs. blank in Ruby
- What is the easiest way to duplicate an activerecord record?
- How to solve error “Missing `secret_key_base` for ‘production’ environment” (Rails 4.1)
- How can I avoid running ActiveRecord callbacks?
- Rails 4: List of available datatypes
- How does instance_eval work and why does DHH hate it?
- How to create a deep copy of an object in Ruby?
- Rails: Access to current_user from within a model in Ruby on Rails
- Is there a clean way to avoid calling a method on nil in a nested params hash? [duplicate]
- Ruby 2.4 and Rails 4 stack level too deep (SystemStackError)
- Custom authentication strategy for devise
- Using Process.spawn as a replacement for Process.fork
- Rails 3, has_one / has_many with lambda condition
- Rails update_attributes without save?
- how to solve “ruby installation is missing psych” error?
- Why are all Rails helpers available to all views, all the time? Is there a way to disable this?
- Rails 3.1 and Ruby 1.9.3p125: ruby-debug19 still crashes with “Symbol not found: _ruby_threadptr_data_type” [duplicate]
- Best Practices for reusing code between controllers in Ruby on Rails
- How to change the default path of view files in a Rails 3 controller?
- Why use “self” to access ActiveRecord/Rails model properties?
- Converting string from snake_case to CamelCase in Ruby
- Converting an array of objects to ActiveRecord::Relation
- Controller spec unknown keyword: id
- What is the difference between pluck and collect in Rails?
- Looking for suggestions for building a secure REST API within Ruby on Rails
- Installing Ruby on Rails – Mac OS Lion
- Ruby: Create range of dates
- Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432?