Class: Philiprehberger::CsvKit::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/csv_kit/detector.rb

Overview

Detects the most likely delimiter for a CSV file by sampling its first lines.

Constant Summary collapse

DELIMITERS =
[',', "\t", ';', '|'].freeze
SAMPLE_LINES =
5

Class Method Summary collapse

Class Method Details

.detect(path_or_io) ⇒ String

Detect the delimiter used in a file or IO.

Parameters:

  • path_or_io (String, IO)

    file path or IO object

Returns:

  • (String)

    the detected delimiter



14
15
16
17
18
19
# File 'lib/philiprehberger/csv_kit/detector.rb', line 14

def self.detect(path_or_io)
  lines = read_sample(path_or_io)
  return ',' if lines.empty?

  DELIMITERS.min_by { |d| variance(lines, d) }
end