BlackHatSamurai
BlackHatSamurai

Reputation: 23483

What is the difference between URI.escape and URI.encode in Ruby?

I'm trying to figure out a difference between a URI.escape and URI.encode in Ruby.

Neither is doing what I want them to, which is to completely encode an URL.

For example I want http://my.web.com to be http%3A%2F%2Fmy%2Eweb%2Ecom

Upvotes: 14

Views: 8492

Answers (2)

maerics
maerics

Reputation: 156434

There is no difference. In Ruby 1.9.3 encode is simply an alias for escape.

[Edit] Note that those methods allow an "unsafe" descriptor of characters to encode:

URI.encode('http://my.web.com', /\W/) # => "http%3A%2F%2Fmy%2Eweb%2Ecom"

Thanks @muistooshort! =)

Upvotes: 14

Thomas
Thomas

Reputation: 181745

CGI.escape almost does what you want:

1.9.3p0 :005 > require 'cgi'
 => true 
1.9.3p0 :006 > CGI.escape 'http://my.web.com'
 => "http%3A%2F%2Fmy.web.com" 

There's usually no point escaping the dots, though.

Upvotes: 3

Related Questions