Project

General

Profile

Actions

Bug #15521

closed

encoding is missed when using `CSV.generate`

Added by sue445 (Go Sueyoshi) almost 6 years ago. Updated almost 6 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.6.0p0
[ruby-dev:50755]

Description

Sample code

require "csv"

rows = [
  ["a", "b", "c", "d", "e"],
  ["あ", "い", "う", "え", "お"],
]

str = CSV.generate do |csv|
  rows.each do |row|
    csv << row
  end
end

p str
p str.encoding

Expected (on Ruby 2.5.3)

$ ruby csv_generate.rb
"a,b,c,d,e\nあ,い,う,え,お\n"
#<Encoding:UTF-8>

Actual (on Ruby 2.6.0)

$ ruby csv_generate.rb
"a,b,c,d,e\n\xE3\x81\x82,\xE3\x81\x84,\xE3\x81\x86,\xE3\x81\x88,\xE3\x81\x8A\n"
#<Encoding:ASCII-8BIT>

This behavior is same to ruby-trunk

$ ruby -v
ruby 2.7.0dev (2019-01-10 trunk 66768) [x86_64-darwin17]

Workaround

Use force_encoding

require "csv"

rows = [
  ["a", "b", "c", "d", "e"],
  ["あ", "い", "う", "え", "お"],
]

str = CSV.generate do |csv|
  rows.each do |row|
    csv << row
  end
end.force_encoding("UTf-8")

p str
p str.encoding

This works, but little dirty...

Updated by kou (Kouhei Sutou) almost 6 years ago

  • Status changed from Open to Closed
  • Assignee set to kou (Kouhei Sutou)

Thanks for your report.
This has been fixed at master: https://github.com/ruby/csv/issues/62

I'll release a new version in a few weeks. Could you try the release (or master) again?

Updated by sue445 (Go Sueyoshi) almost 6 years ago

Thank you.

I used ruby/csv@master, I confirmed this is fixed

Actions #3

Updated by naruse (Yui NARUSE) almost 6 years ago

  • Backport changed from 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN to 2.4: DONTNEED, 2.5: DONTNEED, 2.6: REQUIRED

Updated by kou (Kouhei Sutou) almost 6 years ago

I've updated lib/csv in r66922.

@naruse (Yui NARUSE) Could you backport the revision?

Updated by naruse (Yui NARUSE) almost 6 years ago

  • Backport changed from 2.4: DONTNEED, 2.5: DONTNEED, 2.6: REQUIRED to 2.4: DONTNEED, 2.5: DONTNEED, 2.6: DONE

ruby_2_6 r66926 merged revision(s) 66922.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0