ruby-changes:18337
From: nobu <ko1@a...>
Date: Sat, 25 Dec 2010 18:04:40 +0900 (JST)
Subject: [ruby-changes:18337] Ruby:r30360 (trunk): * lib/csv.rb (CSV.foreach): 'rb' mode is defaulted in open.
nobu 2010-12-25 18:04:33 +0900 (Sat, 25 Dec 2010) New Revision: 30360 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30360 Log: * lib/csv.rb (CSV.foreach): 'rb' mode is defaulted in open. * lib/csv.rb (CSV#init_separators): cannonicalize encoding options as Encoding objects. Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_encodings.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 30359) +++ ChangeLog (revision 30360) @@ -1,3 +1,10 @@ +Sat Dec 25 18:04:30 2010 Nobuyoshi Nakada <nobu@r...> + + * lib/csv.rb (CSV.foreach): 'rb' mode is defaulted in open. + + * lib/csv.rb (CSV#init_separators): cannonicalize encoding options + as Encoding objects. + Sat Dec 25 18:30:34 2010 KOSAKI Motohiro <kosaki.motohiro@g...> * thread.c (rb_thread_atfork): Add small comment why we need Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 30359) +++ lib/csv.rb (revision 30360) @@ -1203,7 +1203,7 @@ # but transcode it to UTF-8 before CSV parses it. # def self.foreach(path, options = Hash.new, &block) - open(path, 'rb', options) do |csv| + open(path, options) do |csv| csv.each(&block) end end @@ -1561,13 +1561,18 @@ # 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 = options.delete(:internal_encoding) || + @encoding = raw_encoding(nil) || + (if encoding = options.delete(:internal_encoding) + case encoding + when Encoding; encoding + else Encoding.find(encoding) + end + end) || (case encoding = options.delete(:encoding) when Encoding; encoding - when /\A[^:]+/; $1 + when /\A[^:]+/; Encoding.find($&) end) || - raw_encoding || Encoding.default_internal || - Encoding.default_external + Encoding.default_internal || Encoding.default_external # # prepare for building safe regular expressions in the target encoding, # if we can transcode the needed characters @@ -2283,7 +2288,7 @@ private - def raw_encoding + def raw_encoding(default = Encoding::ASCII_8BIT) if @io.respond_to? :internal_encoding @io.internal_encoding || @io.external_encoding elsif @io.is_a? StringIO @@ -2291,7 +2296,7 @@ elsif @io.respond_to? :encoding @io.encoding else - Encoding::ASCII_8BIT + default end end end Index: test/csv/test_encodings.rb =================================================================== --- test/csv/test_encodings.rb (revision 30359) +++ test/csv/test_encodings.rb (revision 30360) @@ -260,6 +260,23 @@ end unless encoding == __ENCODING__ rescue Encoding::ConverterNotFoundError end + options[:encoding] = encoding.name + CSV.open(@temp_csv_path, options) do |csv| + csv.each_with_index do |row, i| + assert_equal(fields[i], row) + end + end + options.delete(:encoding) + options[:external_encoding] = encoding.name + options[:internal_encoding] = __ENCODING__.name + begin + CSV.open(@temp_csv_path, options) do |csv| + csv.each_with_index do |row, i| + assert_equal(orig_fields[i], row) + end + end unless encoding == __ENCODING__ + rescue Encoding::ConverterNotFoundError + end end def encode_ary(ary, encoding) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/