# yaml = ...">

In ruby-on-rails, how to convert the '\ X93' format as a string format to its original form?


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 "to_yaml" generate the original look: "你好", I do not mean something like "\ XE4"
Or, anyway, to change the result of "to_yaml" to do this?
Thank!

+3
source share
1 answer

Use ya2yaml :

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

Source: https://habr.com/ru/post/1763322/


All Articles