# Can I know a file is binary or text in ruby?

**URL:** <https://rubytalk.org/t/can-i-know-a-file-is-binary-or-text-in-ruby/767>\
**Category:** ruby-talk\
**Created:** [19 July 2002 16:20 UTC](https://rubytalk.org/t/can-i-know-a-file-is-binary-or-text-in-ruby/767 "2002-07-19T16:20:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mookhae](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Mookhae](https://rubytalk.org/u/Mookhae)\
**Post date:** [19 July 2002 16:20 UTC](https://rubytalk.org/t/can-i-know-a-file-is-binary-or-text-in-ruby/767/1 "2002-07-19T16:20:56Z")

</div>

I am newbie in ruby and computer language.  
To make simple ruby script, I need a file information.  
Which file is binary or text.  
In ruby, how can I know file information?  
I found File class and File::stat class.  
but there is no method which tells file state. (isbinary?)  
Please give me an advice.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Ned\_Konz](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@Ned\_Konz](https://rubytalk.org/u/Ned_Konz)\
**Post date:** [19 July 2002 17:13 UTC](https://rubytalk.org/t/can-i-know-a-file-is-binary-or-text-in-ruby/767/2 "2002-07-19T17:13:06Z")

</div>

Ignoring for a moment what it would mean for a Unicode file to be  
“binary”, you could just do this:

#!/usr/bin/ruby

# From the Perl documentation:

> **···**
>
> On Friday 19 July 2002 09:20 am, mookhae wrote:
> 
> > I am newbie in ruby and computer language.  
> > To make simple ruby script, I need a file information.  
> > Which file is binary or text.  
> > In ruby, how can I know file information?  
> > I found File class and File::stat class.  
> > but there is no method which tells file state. (isbinary?)  
> > Please give me an advice.  
> > Thanks.
> 
> # 
> 
> # The “-T” and “-B” switches work as follows. The
> 
> # first block or so of the file is examined for odd
> 
> # characters such as strange control codes or char-
> 
> # acters with the high bit set. If too many strange
> 
> # characters (\>30%) are found, it’s a “-B” file,
> 
> # otherwise it’s a “-T” file. Also, any file con-
> 
> # taining null in the first block is considered a
> 
> # binary file. If “-T” or “-B” is used on a file-
> 
> # handle, the current stdio buffer is examined
> 
> # rather than the first block. Both “-T” and “-B”
> 
> # return true on a null file, or a file at EOF when
> 
> # testing a filehandle. Because you have to read a
> 
> # file to do the “-T” test, on most occasions you
> 
> # want to use a “-f” against the file first, as in
> 
> # “next unless -f $file && -T $file”.
> 
> # I don’t know how to get to the stdio buffer…
> 
> class File  
> def self.isBinary(name)  
> myStat = stat(name)  
> return false unless myStat.file?  
> open(name) { |file|  
> blk = file.read(myStat.blksize)  
> return blk.size == 0 ||  
> blk.count(“^ -~”, “^\r\n”) / blk.size \> 0.3 ||  
> blk.count(“\x00”) \> 0  
> }  
> end  
> end
> 
> Dir.new(‘.’).each { |entry|  
> if File.stat(entry).file?  
> puts “#{entry} #{ File.isBinary(entry) ? ‘binary’ : ‘text’ }”  
> else  
> puts “#{entry} directory”  
> end  
> }
> 
> –  
> Ned Konz  
> [http://bike-nomad.com](http://bike-nomad.com)  
> GPG key ID: BEEA7EFE

---

<div class="post-metadata">

**Author:** ![GOTO\_Kentaro1](https://avatars.discourse-cdn.com/v4/letter/g/e5b9ba/32.png) [@GOTO\_Kentaro1](https://rubytalk.org/u/GOTO_Kentaro1)\
**Post date:** [19 July 2002 18:02 UTC](https://rubytalk.org/t/can-i-know-a-file-is-binary-or-text-in-ruby/767/3 "2002-07-19T18:02:52Z")

</div>

It is impossible to determine completely whether a file is binary or  
not because binary file is undefined concept. Unix command `file’  
does that by heuristics. file command is pragmatically enough well  
but not complete.

Now, the following tests if a file includes non ascii printable code  
point byte. You can improve this script to detect non latin-1 etc.  
However some coding systems is stateful, for example iso-2022, and  
this approach does not work for such character coding systems.

#! ruby  
NON\_ASCII\_PRINTABLE = /[^\x20-\x7e\s]/

def nonbinary?(io, forbidden, size = 1024)  
while buf = io.read(size)  
return false if forbidden =~ buf  
end  
true  
end

# usage: ruby this\_script.rb filename …

ARGV.each do |fn|  
begin  
open(fn) do |f|  
if nonbinary?(f, NON\_ASCII\_PRINTABLE)  
puts “#{fn}: ascii printable”  
else  
puts “#{fn}: binary”  
end  
end  
rescue  
puts “#$0: #$!”  
end  
end

> **···**
>
> At Sat, 20 Jul 2002 01:20:56 +0900, mookhae wrote:
> 
> > I am newbie in ruby and computer language.  
> > To make simple ruby script, I need a file information.  
> > Which file is binary or text.  
> > In ruby, how can I know file information?
