mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/csv.rb (CSV::foreach, CSV#initialize): directly use encoding
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d05217109f
commit
b4876f5e04
3 changed files with 9 additions and 10 deletions
|
@ -1,4 +1,6 @@
|
||||||
Sat Dec 25 15:58:55 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Dec 25 16:04:34 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* lib/csv.rb (CSV::foreach, CSV#initialize): directly use encoding
|
||||||
|
|
||||||
* lib/csv.rb, test/csv: should not assume $, invariant.
|
* lib/csv.rb, test/csv: should not assume $, invariant.
|
||||||
|
|
||||||
|
|
|
@ -1203,10 +1203,7 @@ class CSV
|
||||||
# but transcode it to UTF-8 before CSV parses it.
|
# but transcode it to UTF-8 before CSV parses it.
|
||||||
#
|
#
|
||||||
def self.foreach(path, options = Hash.new, &block)
|
def self.foreach(path, options = Hash.new, &block)
|
||||||
encoding = options.delete(:encoding)
|
open(path, 'rb', options) do |csv|
|
||||||
mode = "rb"
|
|
||||||
mode << ":#{encoding}" if encoding
|
|
||||||
open(path, mode, options) do |csv|
|
|
||||||
csv.each(&block)
|
csv.each(&block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1564,7 +1561,8 @@ class CSV
|
||||||
# create the IO object we will read from
|
# create the IO object we will read from
|
||||||
@io = data.is_a?(String) ? StringIO.new(data) : data
|
@io = data.is_a?(String) ? StringIO.new(data) : data
|
||||||
# honor the IO encoding if we can, otherwise default to ASCII-8BIT
|
# honor the IO encoding if we can, otherwise default to ASCII-8BIT
|
||||||
@encoding = raw_encoding || Encoding.default_internal ||
|
@encoding = options.delete(:internal_encoding) || options.delete(:encoding) ||
|
||||||
|
raw_encoding || Encoding.default_internal ||
|
||||||
Encoding.default_external
|
Encoding.default_external
|
||||||
#
|
#
|
||||||
# prepare for building safe regular expressions in the target encoding,
|
# prepare for building safe regular expressions in the target encoding,
|
||||||
|
|
|
@ -154,10 +154,9 @@ class TestCSV::Encodings < TestCSV
|
||||||
def test_foreach_allows_you_to_set_encodings
|
def test_foreach_allows_you_to_set_encodings
|
||||||
encode_for_tests([%w[abc def]]) do |data|
|
encode_for_tests([%w[abc def]]) do |data|
|
||||||
# read and write in encoding
|
# read and write in encoding
|
||||||
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
|
File.open(@temp_csv_path, "wb", encoding: data.encoding) { |f| f << data }
|
||||||
CSV.foreach(@temp_csv_path, encoding: data.encoding.name) do |row|
|
CSV.foreach(@temp_csv_path, encoding: data.encoding) do |row|
|
||||||
assert( row.all? { |f| f.encoding == data.encoding },
|
row.each {|f| assert_equal(f.encoding, data.encoding)}
|
||||||
"Wrong data encoding." )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# read and write with transcoding
|
# read and write with transcoding
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue