开发者

In ruby-on-rails, how to convert the '\X93' like string format to its original look?

开发者 https://www.devze.com 2023-01-14 06:55 出处:网络
s = \"你好\" s.encoding# => #<Encoding:UTF-8> yaml = s.to_yaml# => \"--- \\\"\\\\xE4\\\\xBD\\\\xA0\\\\xE5\\\\xA5\\\\xBD\\\"\\n\"

s = "你好"
s.encoding           # => #<Encoding:UTF-8>
yaml = s.to_yaml     # => "--- \"\\xE4\\xBD\\xA0\\xE5\\xA5\\xBD\"\n"
yaml.encoding        # => #<Encoding:ASCII-8BIT>
yaml.force_encoding 'utf-8' # => "--- \"\\xE4\\xBD\\xA0\\xE5\\xA5\\xBD\"\n"

Then, how to make the 'to_yaml' generate original 开发者_StackOverflow社区looking: "你好", I mean not something like '\XE4'

Or, is there anyway to change the 'to_yaml' result to make it?

Thank you!


Use ya2yaml:

require 'ya2yaml'
$KCODE = "UTF8"
"你好".ya2yaml #=> "--- 你好\n"
0

精彩评论

暂无评论...
验证码 换一张
取 消