Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXACT Ruby equivalent of Javascript's escape() function

Consider the string: ` ( ?

Javascript's escape() encodes it like this:

escape("` ( ?")
"%60%20%28%20%3F"

How can I achieve the same effect in Ruby? Nothing I try works:

[Dev]> CGI.escape("` ( ?")
=> "%60+%28+%3F"
[Dev]> URI.encode("` ( ?")
=> "%60%20(%20?"
[Dev]> Addressable::URI.encode("` ( ?")
=> "%60%20(%20?"
like image 443
Tom Lehman Avatar asked Jan 18 '23 23:01

Tom Lehman


1 Answers

ERB::Util.url_encode will do it:

>> require 'erb'
=> true
>> ERB::Util.url_encode("` ( ?")
=> "%60%20%28%20%3F"
like image 140
dnch Avatar answered Feb 03 '23 16:02

dnch