Kamilski81
Kamilski81

Reputation: 15107

How do i ignore line breaks in csv file with ruby but keep a pretty source file?

I have a csv file with about 30 columns that i would like to output, and would like these to appear in my .csv.erb on different lines, for example:

<%= quantity.line_number  %>,
<%= quantity.created_at.strftime("%Y-%m-%d %H:%M:%S") %>,
<%= quantity.partner_entity_no %>,
<%= quantity.partner_name %>,

However when I execute this then my .csv file has line breaks after every ',' I would like to 1) keep each column on separate line in source and 2) have non line breaks in the output .csv file. How could I make this happen?

Upvotes: 1

Views: 361

Answers (2)

dbenhur
dbenhur

Reputation: 20408

Use the stdlib CSV class to get csv quoting and escaping semantics right. Or more conveniently, use its Array#to_csv helper method.

<% require 'csv' %>
...
<%= [ 
      quantity.line_number,
      quantity.created_at.strftime("%Y-%m-%d %H:%M:%S"),
      quantity.partner_entity_no,
      quantity.partner_name,
    ].to_csv %>

Upvotes: 2

Abe Voelker
Abe Voelker

Reputation: 31574

<%= [quantity.line_number,
     quantity.created_at.strftime("%Y-%m-%d %H:%M:%S"),
     quantity.partner_entity_no,
     quantity.partner_name].join(',') %>

Upvotes: 2

Related Questions