先看看这个例子,很多时候我们无法控制 UGC (用户产生的内容),可能会有各种奇葩的情况
Ruby 2.3.0
2.3.0 :010 > url = 'http://ruby-china.org/topics?a=%E6%B5%8B%E8%AF%95#测试'
=> "http://ruby-china.org/topics?a=%E6%B5%8B%E8%AF%95#测试"
2.3.0 :011 > URI.unescape(url)
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
from /Users/jason/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/uri/rfc2396_parser.rb:332:in `gsub'
from /Users/jason/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/uri/rfc2396_parser.rb:332:in `unescape'
from /Users/jason/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/uri/common.rb:126:in `unescape'
from (irb):11
from /Users/jason/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :012 > CGI.unescape(url)
=> "http://ruby-china.org/topics?a=测试#测试"
我这个是在内部 GitLab 的时候发现的问题,结果查下来发现,Ruby 官方在 2009 年就把 URI.decode 给废弃了
- lib/uri/common.rb (URI.escape): obsoleted.
- lib/uri/common.rb (URI,unescape): ditto.
相关连接: