天天看点

Ruby 1.9 String Encoding (WIP)

"你好".force_encoding("GBK") == "你好" #>> false 
"Hello".force_encoding("GBK") == "Hello" #>> true

"你好".force_encoding("GBK") <=> "你好" #>> 1
"Hello".force_encoding("GBK") <=> "Hello" #>> 0

s1 = "a" + "b"
s2 = :a.to_s + "b"
s3 = :a.to_s + "中文"
s1.encoding.name   #>> "UTF-8"
s2.encoding.name   #>> "US-ASCII"
s3.encoding.name   #>> "UTF-8"

irb:
puts "hello".encoding #>> 'UTF-8'

ruby test.rb:
puts "hello".encoding #>> 'US-ASCII'