Created
September 1, 2010 20:53
-
-
Save stepheneb/561350 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.binary?(file) | |
s = File.read(file, 1024) or return false | |
set_encoding(s) | |
begin | |
if s[0, 2] == Marshal.dump('')[0, 2] then | |
true | |
elsif file =~ /erb\.rb$/ then | |
false | |
elsif s.scan(/<%|%>/).length >= 4 || s.index("\x00") then | |
true | |
elsif 0.respond_to? :fdiv then | |
s.count("^ -~\t\r\n").fdiv(s.size) > 0.3 | |
else # HACK 1.8.6 | |
(s.count("^ -~\t\r\n").to_f / s.size) > 0.3 | |
end | |
rescue ArgumentError => e | |
# puts file | |
# puts e | |
# puts caller | |
true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = "/Users/stephen/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/irb/lc/ja/help-message" | |
size = File::Stat.new(file).size | |
puts "file: #{File.basename(file)}, size (bytes) #{size}" | |
puts "reading 1024 bytes into var: f" | |
f = File.read(file, 1024) | |
puts "f.encoding: #{f.encoding}" | |
puts "f.valid_encoding: #{f.valid_encoding?}" | |
if /coding[=:]\s*([^\s;]+)/i =~ f[%r"\A(?:#!.*\n)?.*\n"] | |
if enc = ::Encoding.find($1) | |
puts "f.force_encoding(#{enc})" | |
f.force_encoding(enc) | |
end | |
end | |
puts "f.encoding: #{f.encoding}" | |
puts "f.valid_encoding: #{f.valid_encoding?}" | |
puts | |
f2 = "" | |
f2.force_encoding(enc) | |
puts "f2.length (chars): #{f2.length}" | |
puts "f2.encoding: #{f2.encoding}" | |
puts "f2.valid_encoding: #{f2.valid_encoding?}" | |
begin | |
f.each_char { |c| f2 << c } | |
rescue ArgumentError | |
end | |
puts "f2.length (chars): #{f2.length}" | |
puts "f2.encoding: #{f2.encoding}" | |
puts "f2.valid_encoding: #{f2.valid_encoding?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment