It
returns each row as an array, and uses the * operator to split those arrays into three individual
variables. As you can imagine, you can also use this same technique on data read
from a file, as in the upcoming script in Listing 7-1.
CHAPTER 7 n TRACKING EXPENDITURES WITH PAYPAL 137
FasterCSV can also create CSV from a Ruby array. Here??™s an example of that usage:
require "fastercsv"
secrecy_levels_array = [['SUPERSECRET', 'Supersecret Data', "Tell No One"],
['SEMISECRET', 'Semisecret Data', 'Tell Some People'],
['UNSECRET', 'Unsecret Data', 'Tell Everyone']]
secrecy_levels_array.each do |line|
puts line.to_csv
end
This example has the following output:
SUPERSECRET,Supersecret Data,Tell No One
SEMISECRET,Semisecret Data,Tell Some People
UNSECRET,Unsecret Data,Tell Everyone
This code loops through each element of the array, calls the to_csv method (provided
by FasterCSV) on it, and prints the result. You cannot simply call to_csv on the entire
array because to_csv expects just one row of data at a time.
Pages:
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204