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.

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:
#
# 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
GPG key ID: BEEA7EFE