Forum: Ruby-core [Assigned] Add force_encoding option to csv

F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2014-04-22 05:56
(Received via mailing list)
Issue #9766 has been updated by Nobuyoshi Nakada.

File 0001-csv.rb-honor-encoding-option.patch added
Tracker changed from Feature to Bug
Description updated
Status changed from Open to Assigned
Assignee set to James Gray
ruby -v set to all
Backport set to 2.0.0: REQUIRED, 2.1: REQUIRED

I'd rather think it a bug.

Bug #9766: Add force_encoding option to csv

* Status: Assigned
* Priority: Normal
* Assignee: James Gray
* Category: lib
* Target version: current: 2.2.0
* ruby -v: all
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED
Hi there,

I have a trouble when I use csv#generate with encoding 'Shift-JIS'
option. I investigated it for a long time and found it is caused by
compatibility within "UTF-8" and "Shift-JIS". Since "Shift-JIS" can be
converted to UTF-8, a row with UTF-8 strings added to the csv instance
makes the encoding of whole rows UTF-8.

Take a look at the code below.

Here's the code example.

irb(main):002:0> s = generate(encoding: 'SJIS') do |csv|
    csv << ['あ']
=> ["あ"]

irb(main):003:0> s
=> "あ\n"

irb(main):004:0> s.encoding
=> #<Encoding:UTF-8>

I was intended to make SJIS encoded csv, but the result was UTF-8 csv. I
think everyone think it should generate Shift-JIS encoded csv string, so
could you consider to merge the change attached to this issue?

The expected result is here.

irb(main):002:0> s = generate(encoding: 'SJIS', force_encoding: true) do
    csv << ['あ']
=> ["あ"]

irb(main):003:0> s
=> "\x{E381}\x82\n"

irb(main):004:0> s.encoding
=> #<Encoding:Windows-31J>

csv.rb.diff (1.41 KB)
0001-csv.rb-honor-encoding-option.patch (2.64 KB)
This topic is locked and can not be replied to.