Class: Philiprehberger::CsvKit::Detector
- Inherits:
-
Object
- Object
- Philiprehberger::CsvKit::Detector
- 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
-
.detect(path_or_io) ⇒ String
Detect the delimiter used in a file or IO.
Class Method Details
.detect(path_or_io) ⇒ String
Detect the delimiter used in a file or IO.
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 |