Home

Check if a File is Binary or Text in Ruby

Here's a cool little trick to determining if a file is text or binary in Ruby just by using the path to that file.

Sometimes when you want to read a file in Ruby you need to know whether that file is binary or text, to help you avoid some unwelcome errors.

We can accomplish this by using a gem called ruby-filemagic. We're going to look at the MIME type of the file, and if it's not text, then we'll assume it's binary. We can do something like this:

require 'filemagic'

def text?(filename)
begin
fm = FileMagic.new(FileMagic::MAGIC_MIME)
fm.file(filename) =~ /^text\//
ensure
fm.close
end
end

def binary?(filename)
!text?
end

Then you can run:

> text?("/path/to/my/file.png")
# => false

In addition, since you're typically going to be working with the path of the file (as a string), we could extend the String class to make this a little prettier to write. However, you might want to get more specific with your method naming.

require 'filemagic'

class String

def text_file?
begin
fm = FileMagic.new(FileMagic::MAGIC_MIME)
fm.file(self) =~ /^text\//
ensure
fm.close
end
end

def binary_file?
!text?
end

end

Then you can run this instead:

> "/path/to/my/file.png".text_file?
# => false

> "/path/to/my/file.png".binary_file?
# => true

References:

Let's Connect

Keep Reading

Add a Console to your Ruby Project

A powerful way to debug Rails applications is in using the Rails console. But even when you're not using Rails for your Ruby project, you can still have a console.

Aug 06, 2018

Bulk Resize Images Using Rake and ImageMagick

Got a set of images you need all to conform to the same size? Hate doing it manually? Me too. Let's write a rake task to solve our challenge.

Feb 15, 2016

Add Search to ActsAsTaggableOn with PostgreSQL and Context

ActsAsTaggableOn is a great gem for working with tags in your application, but it can be a PITA when you want to be able to search for tags.

Jan 30, 2017