Module: FileTest

Defined in:
lib/qualitysmith_extensions/file_test/binary_file.rb

Overview

Author

Lloyd Zusman, Tyler Rick

Copyright

Copyright © 2002-2007 Lloyd Zusman

License

Ruby License

Submit to Facets?

Yes

++

Class Method Summary collapse

Class Method Details

._binary_file?(filename) ⇒ Boolean

The text_file? and binary_file? methods are not inverses of each other. Both return false if the item is not a file, or if it is a zero-length file. The “textness” or “binariness” of a file can only be determined if it’s a file that contains at least one byte.

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qualitysmith_extensions/file_test/binary_file.rb', line 24

def self._binary_file?(filename)
  size = self.size(filename)
  blksize = File.stat(filename).blksize
  return nil if !File.file?(filename) || size < 1
  size = [size, blksize].min
  begin
    open(filename) {
      |file|
      block = file.read(size)
      return !self.is_text?(block)
    }
  end
end

.binary_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/qualitysmith_extensions/file_test/binary_file.rb', line 38

def self.binary_file?(filename)
  self._binary_file?(filename)   
end

.text_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/qualitysmith_extensions/file_test/binary_file.rb', line 42

def self.text_file?(filename)
  return_value = self._binary_file?(filename)   
  if return_value.nil?
    return_value 
  else
    !return_value 
  end
end