Sha256: 3756031c8f0d91a354538b25bad9f79fcde5d5a67ed69367851142366d59f7ac
Contents?: true
Size: 914 Bytes
Versions: 63
Compression:
Stored size: 914 Bytes
Contents
require 'dslkit/polite' 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
63 entries across 63 versions & 1 rubygems