URI.escape() broken or misdocumented in Ruby 1.8.4

URI.escape() is supposed to be able to take a second parameter listing
unsafe characters in the URI. This may be a regexp or string. If a
string, it’s supposed to represent a character set listing all unsafe
characters. An example given in the core documentation at:

http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html#M008992

…is:

p URI.escape("@[email protected]!", “!?”)

=> “@%[email protected]%21”

Unfortunately this doesn’t work. It seems that the string does not
represent a character set to match any more, but a literal string to
find - thus the following result is actually seen:

p URI.escape("@[email protected]!", “!?”)

=> “@[email protected]!”

p URI.escape("@[email protected]!", “@?”)

=> “%40%[email protected]!”

Matching strings in this way is thoroughly bizarre as highlighted by the
above example; “@” is being escaped for the substring “@?” but not for
the substring “@!”. The way to get the documented style of substitution
is rather clumsy; one must manually construct the character set:

p URI.escape("@[email protected]!", Regexp.new("[!?]"))

=> “@%[email protected]%21”

I was going to update the documentation Wiki to mention the differing
behaviour in Ruby 1.8.4 by clicking on the link given at the bottom of
the page, which goes to:

http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html#M008992

…but this gives a blank page. In fact the whole Wiki seems to be a bit
broken, with little documentation actually apparently existing and the
search engine failing to find anything at all under “URI”. Am I missing
something here?

Andrew H. wrote:

http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html#M008992

The second time that appeared, it should of course have read:

http://www.ruby-doc.org/ru/wiki/index.rb/Core/URI::Escape

For the sake of searchable archives, further information can be found on
the Ruby list; see:

http://www.ruby-forum.com/topic/72072

p URI.escape("@[email protected]!", Regexp.new("[!?]"))

=> “@%[email protected]%21”

p URI.escape("@[email protected]!", /[!?]/)
#=> “@%[email protected]%21”

It may be misdocumented, but why would you use anything other than a
Regexp here?

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs