Last active
December 10, 2015 01:38
-
-
Save Stormwind/4360816 to your computer and use it in GitHub Desktop.
gsub seems to replace the original encoding of the give sting with the encoding of the gsub content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = 'fo' | |
foo.force_encoding('UTF-8') | |
puts foo.encoding.inspect # #<Encoding:UTF-8> | |
foo2 = foo.gsub(/o/) { |bar| 195.chr } | |
puts foo2.inspect # "f\xC3" | |
puts foo2.length.inspect # 2 | |
puts foo2.encoding.inspect # #<Encoding:ASCII-8BIT> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
foo = 'föo' | |
foo.force_encoding('UTF-8') | |
puts foo.encoding.inspect # #<Encoding:UTF-8> | |
# breakes here | |
foo2 = foo.gsub(/o/) { |bar| "Русский".force_encoding("iso-8859-5") } | |
# never reached | |
puts foo2 | |
puts foo2.inspect | |
puts foo2.length.inspect | |
puts foo2.encoding.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
foo = "Русский".force_encoding("iso-8859-5") | |
puts foo.encoding.inspect # #<Encoding:ISO-8859-5> | |
# breakes here | |
foo2 = foo.gsub(/с/) { |bar| "föö".force_encoding("UTF-8") } | |
# never reached | |
puts foo2 | |
puts foo2.inspect | |
puts foo2.length.inspect | |
puts foo2.encoding.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment