URI.escapeとCGI.escapeの違い

URI.escapeはhttp://の部分はエンコードしない。
CGI.escapeはhttp://の部分も含めてエンコードする。
日本語入りのURLをエンコードするときはURI.escapeを使うのがよい。

require 'uri'
require 'cgi'

str = 'ほげ'

str_uri = URI.escape("http://d.hatena.ne.jp/keyword/#{str}")
str_cgi = CGI.escape("http://d.hatena.ne.jp/keyword/#{str}")

puts str_uri #=> http://d.hatena.ne.jp/keyword/%82%D9%82%B0
puts str_cgi #=> http%3A%2F%2Fd.hatena.ne.jp%2Fkeyword%2F%82%D9%82%B0