Sha256: 9d2244f7a1a8da5ba7da552f65e13bd6fa1358847c08c41bd703fec0d4e8f4b0
Contents?: true
Size: 905 Bytes
Versions: 83
Compression:
Stored size: 905 Bytes
Contents
require 'tins' module Utils module FileXt extend DSLKit::Concern include File::Constants SEEK_SET = File::SEEK_SET ZERO = "\x00" BINARY = "\x01-\x1f\x7f-\xff" if defined?(::Encoding) ZERO.force_encoding(Encoding::ASCII_8BIT) BINARY.force_encoding(Encoding::ASCII_8BIT) end def binary? old_pos = tell seek 0, SEEK_SET data = read 2 ** 13 !data or data.empty? and return nil data.count(ZERO) > 0 and return true data.count(BINARY).to_f / data.size > 0.3 ensure seek old_pos, SEEK_SET end def ascii? case binary? when true then false when false then true end end module ClassMethods def binary?(name) File.open(name, 'rb') { |f| f.binary? } end def ascii?(name) File.open(name, 'rb') { |f| f.ascii? } end end end end
Version data entries
83 entries across 83 versions & 1 rubygems