Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, how do I replace the question mark character in a string?

In Ruby, I have:

require 'uri'
foo = "et tu, brutus?"
bar = URI.encode(foo)      # => "et%20tu,%20brutus?"

I'm trying to get bar to equal "et%20tu,%20brutus%3f" ("?" replaced with "%3F") When I try to add this:

bar["?"] = "%3f"

the "?" matches everything, and I get

=> "%3f"

I've tried

bar["\?"]
bar['?']
bar["/[?]"]
bar["/[\?]"]

And a few other things, none of which work.

like image 757
Olie Avatar asked Feb 13 '09 18:02

Olie


1 Answers

require 'cgi' and call CGI.escape

like image 88
kmkaplan Avatar answered Nov 14 '22 20:11

kmkaplan