How to write tab delimited file when tab delimiter comes from database?

I have a problem really formulating this question, so I'm trying to give an example:

The following code works and produces the expected output: a delimited file in which each column is separated by a “real” tab.

CSV.open(@targetfile, "wb", "\t") { |csv| 
csv << ["row", "of", "CSV", " }

The following code does not produce the expected result.

CSV.open(@targetfile, "wb", @targetdelimiter) { |csv| 
csv << ["row", "of", "CSV", "data"] }

@targetdelimiterin this case comes from the database and is actually a string '\t'(without quotes) that can be configured by the user.

This code also generates splitter output, but I see '\t'instead of the "real" tab character.

What can I do with the second block of code to get the same result as the first code block, given that @targetdelimiter='\t'from db?

+3
2

.

CSV.open(@targetfile, "wb", @targetdelimiter.gsub('\t',"\t")){ |csv| 
csv << ["row", "of", "CSV", "data"] }
+7

\t (ASCII 0x09 Char(9)), . \ t. , , , escape-, - TAB NEWLINE, ( ).

+1

All Articles