Module: Tins::FileBinary::ClassMethods

Defined in:
lib/tins/file_binary.rb

Overview

Class methods that provide file-level binary detection capabilities

These methods allow binary detection without manually opening files

Instance Method Summary collapse

Instance Method Details

#ascii?(name, options = {}) ⇒ Boolean?

Determines if a file is considered ASCII/text by name

Examples:

Basic usage

FileBinary.ascii?('readme.md')  # => true

With custom options

FileBinary.ascii?('binary_file.dat', percentage_zeros: 10.0)  # => false

Parameters:

  • name (String)

    Path to the file to analyze

  • options (Hash) (defaults to: {})

    Configuration options for ASCII detection

Options Hash (options):

  • :offset (Integer) — default: 0

    Starting position in file to begin analysis

  • :buffer_size (Integer) — default: 8192

    Number of bytes to read for analysis

  • :percentage_binary (Float) — default: 30.0

    Binary byte threshold percentage

  • :percentage_zeros (Float) — default: 0.0

    Null byte threshold percentage

Returns:

  • (Boolean, nil)

    true if ASCII/text, false if binary, nil if file is empty



161
162
163
# File 'lib/tins/file_binary.rb', line 161

def ascii?(name, options = {})
  open(name, 'rb') { |f| f.ascii?(options) }
end

#binary?(name, options = {}) ⇒ Boolean?

Determines if a file is considered binary by name

Examples:

Basic usage

FileBinary.binary?('config.json')  # => false

With custom options

FileBinary.binary?('data.dat', percentage_binary: 25.0)  # => true

Parameters:

  • name (String)

    Path to the file to analyze

  • options (Hash) (defaults to: {})

    Configuration options for binary detection

Options Hash (options):

  • :offset (Integer) — default: 0

    Starting position in file to begin analysis

  • :buffer_size (Integer) — default: 8192

    Number of bytes to read for analysis

  • :percentage_binary (Float) — default: 30.0

    Binary byte threshold percentage

  • :percentage_zeros (Float) — default: 0.0

    Null byte threshold percentage

Returns:

  • (Boolean, nil)

    true if binary, false if not binary, nil if file is empty



141
142
143
# File 'lib/tins/file_binary.rb', line 141

def binary?(name, options = {})
  open(name, 'rb') { |f| f.binary?(options) }
end