Yep. But I'd leave the "is_" out - that's handled by the "?" already.
Cheers
robert
···
2007/8/21, Rebhan, Gilbert <Gilbert.Rebhan@huk-coburg.de>:
-----Original Message-----
From: Robert Klemme [mailto:shortcutter@googlemail.com]
Sent: Tuesday, August 21, 2007 9:41 AM
To: ruby-talk ML
Subject: Re: Test if file is binary ?2007/8/21, Alex Gutteridge <alexg@kuicr.kyoto-u.ac.jp>:
> Sorry for the duplicate! Robert is too fast for me./*
It's always good to see more solutions. I like the conciseness of
your solution. But I think this should rather be a class method
because you would not do the test on an open stream. Dunno which of
the solutions is more realistic.
*/you mean it should be something like ? =
class File
def self.is_binary?(name)
ascii = total = 0
File.open(name, "rb") { |io| io.read(1024) }.each_byte do |c|
total += 1;
ascii +=1 if c >= 128 or c == 0
end
ascii.to_f / total.to_f > 0.33
end
end