Closed prashantvithani closed 3 years ago
A patch I can think of:
# csv/writer.rb
class CSV
...
class Writer
...
def header_row?
@use_header && @headers.nil?
end
...
end
end
# csv.rb
class CSV
...
def header_row?
if @writer
writer.header_row?
else
parser.header_row?
end
end
...
end
It's not the best but works for a temporary solution.
cc: @kou
header_row?
is for reader not writer.
Could you show why you want to use header_row?
for writer?
header_row?
is for reader not writer.
Ah, that makes sense. I was using header_row?
on the writer to check whether to write headers or just append the row as is. The row here is a simple ruby hash, the keys (headers) are known while reading the hash.
However, I see the problem in the way I'm using it and checking header_row
? for every row which is being written. What do you think about it?
How about using csv_writer.lineno.zero?
instead of csv_writer.header_row?
?
That works for my use-case, thanks for the suggestion!
OK. I close this.
Definition of
header_row?
returnsparser.header_row?
.Parser
andWriter
are initialized separately and both maintain their own copy of@headers
. So, when a writer writes headers to the file and sets@headers
, it's not updated inParser
andparser.header_row?
always returns true.It worked fine in 1.0.2