LoginSignup
24
22

More than 5 years have passed since last update.

RubyのURI::escapeとCGI::escapeの違い

Last updated at Posted at 2012-10-17

URI::escapeではスラッシュなどはエスケープされない。CGI::escapeではスラッシュなども全てエスケープする。

irb(main):003:0> require 'uri'
=> true
irb(main):004:0> URI.escape("http://example.com/foo/あああ/")
=> "http://example.com/foo/%E3%81%82%E3%81%82%E3%81%82/"
irb(main):005:0> require 'cgi'
=> true
irb(main):006:0> CGI.escape("http://example.com/foo/あああ/")
=> "http%3A%2F%2Fexample.com%2Ffoo%2F%E3%81%82%E3%81%82%E3%81%82%2F"
irb(main):007:0>

組み立てたURIを最後に一括でエスケープするときはURI::escape、http://example.com?redirect=http%3A%2F%2F... のようなURIを組み立てる必要があるときはCGI::escapeでやる、といった感じでいいんだろうか。

24
22
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
22