views:

88

answers:

3

Hi is there an equivalent ruby method to JavaScript encodeURIComponent method? i am using the URI.unescape(str) but it recognizes the "£" (after encodeURIComponent it becomes "%C2%A3") as a "?" sign. any solution's? thanks

+1  A: 

URI.escape

Darin Dimitrov
lets say i have a str = "£" i use encodeURIComponent(str) in javascript, but when i use URI.unescape(str) in ruby and that string comes back as a str = "?". so there are differences.
Mo
+2  A: 
URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

found here: http://stackoverflow.com/questions/2834034/how-do-i-raw-url-encode-decode-in-javascript-and-ruby-to-get-the-same-values-in-b

dierre
lets say i have a str = "£" i use encodeURIComponent(str) in javascript, but when i use URI.unescape(str) in ruby and that string comes back as a str = "?". so there are differences
Mo
A: 

CGI.escape should escape correctly, except that spaces are escaped as +.

Note that URI.escape has been deprecated in Ruby 1.9.2...

There is a long discussion on ruby-core for those interested.

Marc-André Lafortune