Class: FbUtils::Writer::Csv

Inherits:
Base
  • Object
show all
Defined in:
lib/fb_utils/writers/csv.rb

Instance Attribute Summary

Attributes inherited from Base

#filename, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from FbUtils::Writer::Base

Instance Method Details

#write(arr) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fb_utils/writers/csv.rb', line 4

def write(arr)
  @options = {
    :fields_terminated_by => ";", 
    :fields_enclosed_by => "\"", 
    :lines_terminated_by => "\n"
  }.merge(@options)

  File.open(@filename, "w") do |f|
    arr.each{ |a|
      fields = a.to_array.collect{|x| x.surround(@options[:fields_enclosed_by])} 
      fields = fields.join(@options[:fields_terminated_by])
      fields.insert(-1, @options[:lines_terminated_by])
      f.puts fields 
    }
  end
end