From: GOTO Kentaro Date: 2002-07-20T03:02:52+09:00 Subject: Re: Can I know a file is binary or text in ruby? 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? 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