ruby-changes:18331
From: nobu <ko1@a...>
Date: Sat, 25 Dec 2010 16:04:44 +0900 (JST)
Subject: [ruby-changes:18331] Ruby:r30354 (trunk): * lib/csv.rb (CSV::foreach, CSV#initialize): directly use encoding
nobu 2010-12-25 16:04:38 +0900 (Sat, 25 Dec 2010) New Revision: 30354 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30354 Log: * lib/csv.rb (CSV::foreach, CSV#initialize): directly use encoding Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_encodings.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 30353) +++ ChangeLog (revision 30354) @@ -1,5 +1,7 @@ -Sat Dec 25 15:58:55 2010 Nobuyoshi Nakada <nobu@r...> +Sat Dec 25 16:04:34 2010 Nobuyoshi Nakada <nobu@r...> + * lib/csv.rb (CSV::foreach, CSV#initialize): directly use encoding + * lib/csv.rb, test/csv: should not assume $, invariant. Sat Dec 25 16:08:06 2010 KOSAKI Motohiro <kosaki.motohiro@g...> Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 30353) +++ lib/csv.rb (revision 30354) @@ -1203,10 +1203,7 @@ # but transcode it to UTF-8 before CSV parses it. # def self.foreach(path, options = Hash.new, &block) - encoding = options.delete(:encoding) - mode = "rb" - mode << ":#{encoding}" if encoding - open(path, mode, options) do |csv| + open(path, 'rb', options) do |csv| csv.each(&block) end end @@ -1564,7 +1561,8 @@ # create the IO object we will read from @io = data.is_a?(String) ? StringIO.new(data) : data # 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 # # prepare for building safe regular expressions in the target encoding, Index: test/csv/test_encodings.rb =================================================================== --- test/csv/test_encodings.rb (revision 30353) +++ test/csv/test_encodings.rb (revision 30354) @@ -154,10 +154,9 @@ def test_foreach_allows_you_to_set_encodings encode_for_tests([%w[abc def]]) do |data| # read and write in encoding - File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data } - CSV.foreach(@temp_csv_path, encoding: data.encoding.name) do |row| - assert( row.all? { |f| f.encoding == data.encoding }, - "Wrong data encoding." ) + File.open(@temp_csv_path, "wb", encoding: data.encoding) { |f| f << data } + CSV.foreach(@temp_csv_path, encoding: data.encoding) do |row| + row.each {|f| assert_equal(f.encoding, data.encoding)} end # read and write with transcoding -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/