From: Eric Hodel Date: 2011-01-29T09:54:32+09:00 Subject: [ruby-core:34959] [Ruby 1.9-Bug#4340][Open] Encoding of result string for String#gsub is not consistent Bug #4340: Encoding of result string for String#gsub is not consistent http://redmine.ruby-lang.org/issues/show/4340 Author: Eric Hodel Status: Open, Priority: Normal Category: core ruby -v: ruby 1.9.3dev (2011-01-26 trunk 30659) [x86_64-darwin10.6.0] Depending upon where the replacement occurs, the encoding of the result of String#gsub is not consistent. When the replacement happens at the beginning of the string the encoding of the result is the encoding of the replacement string. When the replacement happens elsewhere in the string the encoding of the result is the result of the original string. With String#sub the encoding of the result is the encoding of the original string always. $ cat t.rb puts 'using gsub' hello_world = 'Hello World!' hello_world.force_encoding Encoding::UTF_8 everybody = 'Everybody' everybody.force_encoding Encoding::US_ASCII hello_everybody = hello_world.gsub(/World/, 'Everybody') p hello_everybody p hello_everybody.encoding hi = 'Hi' hi.force_encoding Encoding::US_ASCII hi_world = hello_world.gsub(/Hello/, 'Hi') p hi_world p hi_world.encoding puts 'using sub' hello_world = 'Hello World!' hello_world.force_encoding Encoding::UTF_8 everybody = 'Everybody' everybody.force_encoding Encoding::US_ASCII hello_everybody = hello_world.sub(/World/, 'Everybody') p hello_everybody p hello_everybody.encoding hi = 'Hi' hi.force_encoding Encoding::US_ASCII hi_world = hello_world.sub(/Hello/, 'Hi') p hi_world p hi_world.encoding $ ruby19 -v t.rb ruby 1.9.3dev (2011-01-26 trunk 30659) [x86_64-darwin10.6.0] using gsub "Hello Everybody!" # "Hi World!" # using sub "Hello Everybody!" # "Hi World!" # ---------------------------------------- http://redmine.ruby-lang.org