6

What I do

str= "  John   Smith Beer "
str.tr(" ", "%20")
"%%John%%%Smith%Beer%"

It doesn't replace it with "%20". How to fix it?

Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133
  • 2
    Assuming this is to be used for a URL ... you could just leave the space in. Browsers will do this conversion for you before submitting the request. Otherwise - the solution from MilesStanfield is most appropriate. – Jon Jan 06 '16 at 08:39
  • Rails usually handles this conversion. What are you trying to do? – Stefan Jan 06 '16 at 08:40
  • Yes on browser its work...Issue which I face is that this is ans API and on mobile video this doesn't work because of spaces... – Haseeb Ahmad Jan 06 '16 at 09:37

1 Answers1

20
require 'uri'
URI.encode("green books")
# => "green%20books"
MilesStanfield
  • 4,571
  • 1
  • 21
  • 32