Escaping URI strings the Ruby way.

by Rabbit

You could say:

URI.encode('hello world') # hello%20world

But that’s ugly, so why not:

'hello world'.encode # hello%20world

Much nicer and more rubyesque!

Here’s one way to do it:

require 'uri'
 
class String
 
  def encode
    URI.encode(self)
  end
 
  def decode
    URI.decode(self)
  end
 
end

Why do you use decode and encode instead of escape and unescape?

Because encode and decode make more sense to me. :)