tags:

views:

452

answers:

2

I try to modify "/foo/bar/dir" -> "\/foo\/bar\/dir" before execute it in command line.

I test by using gsub in irb the result is

x = "/foo/bar/dir"

x.gsub("/","\\\/")

=> "\\\/foo\\\/bar\\\/dir"

x.gsub("/","\/")

=> "/foo/bar/dir"

Is it possible to replace "/" with "\/" by gsub ?


Source of problems is

I try to execute "string in command line" and "real_path" is my variable

real_path = "/home/me/www/idata"

path = real_path.gsub("/","\\\/")

puts path

=> \/home\/me\/www\/idata 

run "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"

result from "run" command is

"sh -c 'sed '\\''s/SHARE_PATH/\\\/home\\\/me\\\/www\\\/idata\\\/shared/g  .... "

I need is only one back slash like

"sh -c 'sed '\\''s/SHARE_PATH/\\/home\\/me\\/www\\/idata\\/shared/g  .... "

"run" is command from Capistrano

my solution is

use single quote instead of double quote like this

path = real_path.gsub("/",'\/') 
+2  A: 

Yes

irb(main):028:0> (t = x.gsub("/", "\\/")) && nil
=> nil
irb(main):029:0> t
=> "\\/foo\\/bar\\/dir"
irb(main):030:0> puts t
\/foo\/bar\/dir
=> nil

Your first example actually did what you wanted, but the .inspect method that irb is using is escaping backslashes, so it looked like there were extras. If you had used puts you would have seen the real result.

DigitalRoss
+3  A: 

You have written:

x = "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"

so You did what You had asked before. x.gsub("/","\\/") in fact evaluates to "\/foo\/bar\/dir" but irb prints return value of inspect method instead of to_s.

Edit: Did You mean

real_path.gsub("/","\/")

istead of

real_path.gsub("\/","\/")

Anyway the output is correct - You changed / with \/ so You have

"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g'\'' .... "`

instead of

`"sh -c 'sed '\''s/SHARE_PATH//home/me/www/idata/shared/g'\'' .... "`

and result is different from irb's result (notice the lack of doubled backslash).

For path manipulation I recommend using File.join (documentation)

By the way: why are You modifying the path this way? (1)

Edit2: Why are You asking about changing "/" to "\/" but write the following line?

path = real_path.gsub("\/","\\/") 

What are You trying to achieve? And what is Your answer to question (1) ?

Edit3:

Here We go:

>> real_path = "/foo/bar/dir"
=> "/foo/bar/dir"
>> path = real_path.gsub("/", "\\/")
=> "\\/foo\\/bar\\/dir"
>> puts "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
sed 's/SHARE_PATH/\/foo\/bar\/dir/g' \/foo\/bar\/dir/config/sphinx.yml > \/foo\/bar\/dir/config/sphinx.tmp.yml
=> nil
>>

but I do not understand why You need backslash in a path?

Dejw
(1) I try to create "command line string" that use in "sed" command it's need directory that look like "\/foo\/bar\/dir" and full command look like -> run "sed 's/SHARE_PATH/\/foo\/bar\/dir/g' xxx.txt > yyy.txt"
Bank
(last) sed command need string 's/<string 1>/<string 2>/g' That why I need back slash in my directory path.
Bank
All right then - I gave You the answer about 20min ago.
Dejw