From: Reprisal Date: 2006-10-01T12:13:59+09:00 Subject: Re: How to escape spaces Your trouble isn't with your gsub patterns I don't believe but with what you are seeing in irb. irb(main):107:0> t="a B" => "a B" irb(main):108:0> t.gsub(/ /,"\\") => "a\\B" irb(main):109:0> puts t.gsub(/ /,"\\") a\B => nil irb(main):110:0> 92.chr => "\\" irb(main):111:0> puts 92.chr \ => nil irb(main):112:0> Regexp::escape(t) => "a\\ B" irb(main):113:0> puts Regexp::escape(t) a\ B => nil On Sep 30, 2006, at 9:30 PM, Jonathan Wallace wrote: > I'm using ruby 1.8.4. I'm trying to use the gsub function to escape > spaces in a linux path that is stored in a variable. > > When I attempt the following, t = "a B"; t.gsub(/ /, "\\ "), I receive > the following output: > > irb(main):001:0> t = "a B"; t.gsub(/ /, "\\ ") > => "a\\ B" > > I'd like to get "a\ B" (without quotes, of course). Can someone point > me in the proper direction? Note: I've tried many different > variations > with no luck. > Thanks, > Jonathan > >