# URI.escape() broken or misdocumented in Ruby 1.8.4?

**URL:** <https://www.ruby-forum.com/t/uri-escape-broken-or-misdocumented-in-ruby-1-8-4/97433>\
**Category:** Ruby\
**Created:** [July 7, 2006, 1:27am UTC](https://www.ruby-forum.com/t/uri-escape-broken-or-misdocumented-in-ruby-1-8-4/97433 "2006-07-07T01:27:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [July 7, 2006, 1:27am UTC](https://www.ruby-forum.com/t/uri-escape-broken-or-misdocumented-in-ruby-1-8-4/97433/1 "2006-07-07T01:27:31Z")

</div>

I posted this in the Ruby On Rails forum a moment ago because of other  
URI.escape() related messages there, but really it’s a Ruby thing, not a  
Rails thing, so it belongs in the Ruby forum. Thus, a repost 🙂

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](http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI/Escape.html#M008992)

…is:

p URI.escape(“@?@!”, “!?”)

# =\> “@%3F@%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(“@?@!”, “!?”)

# =\> “@?@!”

p URI.escape(“@?@!”, “@?”)

# =\> “%40%3F@!”

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(“@?@!”, Regexp.new(“[!?]”))

# =\> “@%3F@%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/ru/wiki/index.rb/Core/URI::Escape](http://www.ruby-doc.org/ru/wiki/index.rb/Core/URI::Escape)

…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?

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [July 7, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/uri-escape-broken-or-misdocumented-in-ruby-1-8-4/97433/2 "2006-07-07T16:59:40Z")

</div>

On 7/6/06, Andrew H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I posted this in the Ruby On Rails forum a moment ago because of other  
> URI.escape() related messages there, but really it’s a Ruby thing, not a  
> Rails thing, so it belongs in the Ruby forum. Thus, a repost 🙂

I don’t have a clear answer to your question, below, but a couple of  
points that _MUST_ be made: You are not posting to a forum with  
_either_ the “Ruby on Rails ‘forum’” or the “Ruby ‘forum’”. You are  
posting to a mailing list gateway. You are correct that this probably  
belongs on ruby-talk (the mailing list to which your post has been  
gatewayed) and comp.lang.ruby (the newsgroup to which the mailing list  
is bidirectionally gatewayed). But it’s not a forum.

It does appear to be a documentation bug or a code bug that was  
introduced; someone else will have to verify that.

> 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/ru/wiki/index.rb/Core/URI::Escape](http://www.ruby-doc.org/ru/wiki/index.rb/Core/URI::Escape)

I didn’t even know about this 😉

I suspect that it’s not filled because it’s not how the documentation  
is dealt with in Ruby.

Lastly, your example:

p URI.escape(“@?@!”, Regexp.new(“[!?]”))

# =\> “@%3F@%21”

Can be rewritten as:

p URI.escape(“@?@!”, /[!?]/)

You don’t need Regexp.new.

-austin

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [July 7, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/uri-escape-broken-or-misdocumented-in-ruby-1-8-4/97433/3 "2006-07-07T17:17:32Z")

</div>

“Austin Z.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> It does appear to be a documentation bug or a code bug that was  
> introduced; someone else will have to verify that.

This seems like a documentation bug. The second argument is used as-is  
in calling String#gsub which matches whole-string when given a string  
argument.

Note that the documentation in 1.8.4’s uri library code is correct.

YS.
