Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created February 21, 2014 16:57
Show Gist options
  • Save djberg96/9138244 to your computer and use it in GitHub Desktop.
Save djberg96/9138244 to your computer and use it in GitHub Desktop.
File.binary? method for Ruby
# Mostly works, but is returning true on jpg images.
class File
def self.binary?(file)
s = (File.read(file, File.stat(file).blksize) || "")
s = s.encode('US-ASCII', :undef => :replace).split(//)
((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment