diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-06-10 01:57:10 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-06-10 01:57:10 +0000 |
commit | f9a5335ed4f84e6e3ed23cb92966a36101d9d149 (patch) | |
tree | 8ddbd447fd245dfb2377047fda71cde13f94e408 /lib | |
parent | 47c64fdf5447bdde3de074855e4867a349f4a3b5 (diff) | |
download | ruby-f9a5335ed4f84e6e3ed23cb92966a36101d9d149.tar.gz |
csv.rb: honor encoding option
* lib/csv.rb (CSV#<<): honor explicity given encoding. based on
the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at
[ruby-core:62113]. [Bug #9766]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46391 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/csv.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index e1199185f9..60b22e76ba 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1148,9 +1148,9 @@ class CSV io.seek(0, IO::SEEK_END) args.unshift(io) else - encoding = (args[-1] = args[-1].dup).delete(:encoding) if args.last.is_a?(Hash) + encoding = args[-1][:encoding] if args.last.is_a?(Hash) str = "" - str.encode!(encoding) if encoding + str.force_encoding(encoding) if encoding args.unshift(str) end csv = new(*args) # wrap @@ -1524,7 +1524,7 @@ class CSV init_headers(options) init_comments(options) - options.delete(:encoding) + @force_encoding = !!(encoding || options.delete(:encoding)) options.delete(:internal_encoding) options.delete(:external_encoding) unless options.empty? @@ -1664,10 +1664,13 @@ class CSV output = row.map(&@quote).join(@col_sep) + @row_sep # quote and separate if @io.is_a?(StringIO) and - output.encoding != raw_encoding and - (compatible_encoding = Encoding.compatible?(@io.string, output)) - @io.set_encoding(compatible_encoding) - @io.seek(0, IO::SEEK_END) + output.encoding != (encoding = raw_encoding) + if @force_encoding + output = output.encode(encoding) + elsif (compatible_encoding = Encoding.compatible?(@io.string, output)) + @io.set_encoding(compatible_encoding) + @io.seek(0, IO::SEEK_END) + end end @io << output |