From: Nobuyoshi Nakada Date: 2007-06-20T03:16:36+09:00 Subject: Re: Check for text file Hi, At Wed, 20 Jun 2007 02:10:57 +0900, Ryan Davis wrote in [ruby-talk:256206]: > > After some research I still cannot find a way how to see if a file is > > plain text or binary. In fact I want to check if a file is plain > > text no > > matter what characters are in it. > > http://blog.zenspider.com/archives/2006/08/i_miss_perls_b.html You can use String#count: def File.binary?(path) s = read(path, 4096) and !s.empty? and (/\0/n =~ s or s.count("\t\n -~").to_f/s.size<=0.7) end In any case, it doesn't work for non-ascii files. -- Nobu Nakada