Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array-to-csv.rb

Instance Method Summary collapse

Instance Method Details

#to_csvObject


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/array-to-csv.rb', line 2

def to_csv
  tmp = []
  
  for i in self
    # todo research case syntax, since these elsifs are ugly
    # todo investigate switching to %Q syntax to make less ugly
    # todo refactor out the duplicate gsubs
    if i.is_a? Array
      tmp << i.map{|x| "\"#{x.to_s.escape_csv_quotes}\""}.join(',')
    elsif i.is_a? String
      tmp << "\"#{i.escape_csv_quotes}\""
    elsif i.is_a? Hash
      tmp << i.map{|k, v| "\"#{k.to_s.escape_csv_quotes}: #{v.to_s.escape_csv_quotes}\""}.join(',')
    end
  end
  
  tmp.join("\n")
end