Class: Knj::Csv

Inherits:
Object show all
Defined in:
lib/knj/csv.rb

Overview

Contains various methods for handeling CSV-stuff.

Class Method Summary collapse

Class Method Details

.arr_to_csv(arr, del, encl) ⇒ Object

Converts a given array to a CSV-string.

Examples

str = Knj::Csv.arr_to_csv([1, 2, 3], “;”, “‘”) #=> “’1’;‘2’;‘3’n”



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/knj/csv.rb', line 6

def self.arr_to_csv(arr, del, encl)
  raise "No delimiter given." if !del
  raise "No enclosure given." if !encl
  
  str = ""
  first = true
  arr.each do |val|
    if first
      first = false
    else
      str << del
    end
    
    val = val.to_s.encode("utf-8").gsub(del, "").gsub(encl, "")
    str << "#{encl}#{val}#{encl}"
  end
  
  str << "\n"
  
  return str
end