views:

17

answers:

1

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 looking: "你好", I mean not something like '\XE4'
Or, is there anyway to change the 'to_yaml' result to make it?
Thank you!

+1  A: 

Use the gem ya2yaml:

>> require 'ya2yaml'
>> $KCODE = "UTF8"
>> s = "你好"
>> s.ya2yaml
=> "--- 你好\n"
tokland