Skip to content

Instantly share code, notes, and snippets.

@alanstevens
Created October 3, 2013 17:11
Show Gist options
  • Save alanstevens/6813382 to your computer and use it in GitHub Desktop.
Save alanstevens/6813382 to your computer and use it in GitHub Desktop.
Ruby encoding is fun!
[29] pry(main)> foo = 'a&b*c d'
=> "a&b*c d"
[30] pry(main)> URI::escape(foo, /[^a-zA-Z0-9\-\.\_\~]/)
=> "a%26b%2Ac%20d"
[31] pry(main)> uri_val = URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
=> "a%26b*c%20d"
[32] pry(main)> CGI.escape(foo)
=> "a%26b%2Ac+d"
[33] pry(main)> URI.escape(foo)
=> "a&b*c%20d"
@codatory
Copy link

codatory commented Oct 3, 2013

You should try [].

>> foo = '[]a&b*c d'
=> "[]a&b*c d"
>> URI::escape(foo, /[^a-zA-Z0-9\-\.\_\~]/)
=> "%5B%5Da%26b%2Ac%20d"
>> URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
=> "%5B%5Da%26b*c%20d"
>> CGI.escape(foo)
=> "%5B%5Da%26b%2Ac+d"
>> URI.escape(foo)
=> "[]a&b*c%20d"

@alanstevens
Copy link
Author

See? It's so simple! :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment