From: Gary Watson Date: 2009-12-27T19:04:18+09:00 Subject: Re: ruby 1.9 hates you and me and the encodings we rode in on so just get used to it. Thanks for the suggestion of using ascii-8bit. This solved my problem. The line of code that was giving me fits was the following line. Worked in 1.8 but didn't work in 1.9.0 puts Dir["**/*"].select {|x| x.match(/(jpg)$/)} when I changed it to this puts Dir["**/*"].select {|x| x.force_encoding("ascii-8bit").match(/(jpg)$/)} all was well. Regards, Gary Yukihiro Matsumoto wrote: > Hi, > > In message "Re: ruby 1.9 hates you and me and the encodings we rode in > on so just get used to it." > on Sat, 17 May 2008 06:10:05 +0900, DJ Jazzy Linefeed > writes: > | > |def prep_file(path) > | > | ret = '' > | > | x = File.open(path) > | > | x.lines.each do |l| > | l.gsub!('\n', ' ') > | ret << l > | end > | > | puts ret > | > |end > |... > |compare.rb:64:in `gsub': broken UTF-8 string (ArgumentError) > | from compare.rb:64:in `block in prep_file' > | from compare.rb:63:in `each_line' > | from compare.rb:63:in `call' > | from compare.rb:63:in `each' > | from compare.rb:63:in `prep_file' > | from compare.rb:144:in `
' > > Regular expression operation does not work fine on broken strings. It > seems that you specify utf-8 for your locale, yet the content of > reading file is not. If you know the encoding of the content, say > iso-8859-1, you can open it with the explicit encoding: > > x = File.open(path, "r:iso-8859-1") > > if not, you can say it > > x = File.open(path, "r:ascii-8bit") > > unless the file content is non ASCII like UTF-16. > > matz. -- Posted via http://www.ruby-forum.com/.