Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :sep option as alias for :col_sep and TSV class for tab-separated values #317

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,7 @@ def create_stringio(str, mode, opts)
#
def initialize(data,
col_sep: ",",
sep: nil,
row_sep: :auto,
quote_char: '"',
field_size_limit: nil,
Expand All @@ -2059,6 +2060,11 @@ def initialize(data,
write_empty_value: "")
raise ArgumentError.new("Cannot parse nil as CSV") if data.nil?

if sep && col_sep != ","
raise ArgumentError, "Cannot specify both :sep and :col_sep"
end
actual_sep = sep || col_sep

if data.is_a?(String)
if encoding
if encoding.is_a?(String)
Expand Down Expand Up @@ -2094,7 +2100,7 @@ def initialize(data,
max_field_size = field_size_limit - 1
end
@parser_options = {
column_separator: col_sep,
column_separator: actual_sep,
row_separator: row_sep,
quote_character: quote_char,
max_field_size: max_field_size,
Expand Down Expand Up @@ -2129,6 +2135,93 @@ def initialize(data,
writer if @writer_options[:write_headers]
end

class TSV < CSV
def initialize(data, **options)
options = options.dup
if !options.key?(:col_sep) && !options.key?(:sep)
options[:col_sep] = "\t"
end
super(data, **options)
end

class << self
def foreach(path, mode="r", **options, &block)
options = options.dup
if !options.key?(:col_sep) && !options.key?(:sep)
options[:col_sep] = "\t"
end
CSV.foreach(path, mode, **options, &block)
end

def read(path, **options)
options = options.dup
if !options.key?(:col_sep) && !options.key?(:sep)
options[:col_sep] = "\t"
end
CSV.read(path, **options)
end

def parse(str, **options)
options = options.dup
if !options.key?(:col_sep) && !options.key?(:sep)
options[:col_sep] = "\t"
end
CSV.parse(str, **options)
end

def generate(str = nil, **options)
options = with_default_separator(options)
CSV.generate(str, **options)
end

def open(filename, mode="rb", **options, &block)
options = with_default_separator(options)
CSV.open(filename, mode, **options, &block)
end

def table(path, **options)
options = with_default_separator(options)
CSV.table(path, **options)
end

def parse_line(line, **options)
options = with_default_separator(options)
CSV.parse_line(line, **options)
end

def generate_line(row, **options)
options = with_default_separator(options)
CSV.generate_line(row, **options)
end

def instance(data = nil, **options, &block)
options = with_default_separator(options)
CSV.instance(data, **options, &block)
end

def filter(input=nil, output=nil, **options, &block)
options = with_default_separator(options)
CSV.filter(input, output, **options, &block)
end

def readlines(path, **options)
options = with_default_separator(options)
CSV.readlines(path, **options)
end

private

def with_default_separator(options)
options = options.dup
if !options.key?(:col_sep) && !options.key?(:sep)
options[:col_sep] = "\t"
end
options
end
end
end
end

# :call-seq:
# csv.col_sep -> string
#
Expand Down