I have problem with writng “\4” into a file, for example, I want to
write a string “aaa\4024_folder\mm” into a file test.cfg. But once the
string wrote into file, it is “aaa024_folder\mm”. So, i do this,
string=“aaa\4024_folder\mm”
string=string.sub(/\4/,"\4") the result is the same.
I tried string=string.sub(/\4/,"\4"), the result is showed a square
Any one has any idea to solve the problem? Thank you in advance.
I have problem with writng “\4” into a file, for example, I want to
write a string “aaa\4024_folder\mm” into a file test.cfg. But once the
string wrote into file, it is “aaa024_folder\mm”. So, i do this,
string=“aaa\4024_folder\mm”
string=string.sub(/\4/,"\4") the result is the same.
I tried string=string.sub(/\4/,"\4"), the result is showed a square
Any one has any idea to solve the problem? Thank you in advance.
I have problem with writng “\4” into a file, for example, I want to
write a string “aaa\4024_folder\mm” into a file test.cfg. But once the
string wrote into file, it is “aaa024_folder\mm”. So, i do this,
string=“aaa\4024_folder\mm”
string=string.sub(/\4/,"\4") the result is the same.
I tried string=string.sub(/\4/,"\4"), the result is showed a square
Any one has any idea to solve the problem? Thank you in advance.
Otherwise, keeping adding slashes to your match while trying to replace
it with “HELLO”. Don’t be surprised if it takes 9 slashes to get the
match. Once you get the match, then use your desired replacement and
keep adding slashes to it until it works.
I have problem with writng “\4” into a file, for example, I want to
write a string “aaa\4024_folder\mm” into a file test.cfg. But once the
string wrote into file, it is “aaa024_folder\mm”.
That’s because \4 inside a double-quoted string is a single character,
with code 4.
If you want a backslash inside a double-quoted string, put two
backslashes.
If you are building filenames, note that even under Windows you can use
forward-slashes inside Ruby.
Inside a gsub replacement string it can be even more confusing, since a
backslash followed by 4 means “replace this with the fourth capture in
the match”. So to replace x with \4, you need to do this
irb(main):003:0> str = “wxyz”
=> “wxyz”
irb(main):004:0> res = str.gsub(/x/, “\\4”)
=> “w\4yz”
irb(main):005:0> puts res
w\4yz
=> nil
Or use the block form of gsub, which doesn’t treat backslash-N
specially.
irb(main):007:0> res = str.gsub(/x/) { “\4” }
=> “w\4yz”
irb(main):008:0> puts res
w\4yz
=> nil
Otherwise, keeping adding slashes to your match while trying to replace
it with “HELLO”. Don’t be surprised if it takes 9 slashes to get the
match. Once you get the match, then use your desired replacement and
keep adding slashes to it until it works.
Thanks. After puting 8 slashes, the path is correctely written in the
file…speachless…
Thanks. After puting 8 slashes, the path is correctely written in the
file…speachless…
Then there is something else which is doing a third level of dequoting.
I can’t say what this is, since you don’t show your code.
Perhaps you are passing the string to a shell, e.g. using backticks,
%x{…}, system(), IO.popen or similar. In this case, the shell also
treats backslash sequences specially.
Here, str is actually two characters (backslash followed by backslash).
But the shell treats this as a single backslash-escaped character.
You can avoid this problem by not getting the shell involved. If you
pass multiple arguments to system() then it invokes the target program
with the arguments exactly as given, without invoking the shell at all.
irb(main):004:0> system(“echo”, str)
\
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.