# Question to write \\4 into file

**URL:** <https://www.ruby-forum.com/t/question-to-write-4-into-file/167590>\
**Category:** Ruby\
**Created:** [May 9, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590 "2009-05-09T20:46:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 9, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/1 "2009-05-09T20:46:58Z")

</div>

Hi there,

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.

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 9, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/2 "2009-05-09T21:56:02Z")

</div>

Cheyne Li wrote:

> Hi there,
> 
> 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.

Use single quotes around your string?

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 11, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/3 "2009-05-11T21:47:07Z")

</div>

7stud – wrote:

> Cheyne Li wrote:
> 
> > Hi there,
> > 
> > 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.
> 
> Use single quotes around your string?

I tried ‘\4’ ‘\4’ not luck at all

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 9, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/4 "2009-05-09T21:58:38Z")

</div>

7stud – wrote:

> Use single quotes around your string?

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.

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 11, 2009, 10:25pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/5 "2009-05-11T22:25:55Z")

</div>

Cheyne Li wrote:

> Hi there,
> 
> 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.

irb(main):001:0\> str = “aaa\4024\_folder\mm”  
=\> “aaa\4024\_folder\mm”  
irb(main):002:0\> puts str  
aaa\4024\_folder\mm  
=\> nil

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

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 11, 2009, 10:25pm UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/6 "2009-05-11T22:25:05Z")

</div>

On May 11, 2009, at 3:47 PM, Cheyne Li wrote:

> > > 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.
> > 
> > Use single quotes around your string?
> 
> ## I tried ‘\4’ ‘\4’ not luck at all

as a literal string, you need to escape the \

string = “aaa\4024\_folder\mm”  
File.open(‘test.cfg’, ‘a’) {|f| f.puts string }

This appends the string to the test.cfg file.

://tmp $ irb  
irb\> string = “aaa\4024\_folder\mm”  
=\> “aaa\4024\_folder\mm”  
irb\> File.open(‘test.cfg’, ‘a’) {|f| f.puts string }  
=\> nil  
irb\> exit  
://tmp $ cat test.cfg  
aaa\4024\_folder\mm

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 12, 2009, 12:04am UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/7 "2009-05-12T00:04:56Z")

</div>

7stud – wrote:

> 7stud – wrote:
> 
> > Use single quotes around your string?
> 
> 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…

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [May 12, 2009, 9:37am UTC](https://www.ruby-forum.com/t/question-to-write-4-into-file/167590/8 "2009-05-12T09:37:40Z")

</div>

Cheyne Li wrote:

> 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.

irb(main):001:0\> str = “\\”  
=\> “\\”  
irb(main):002:0\> puts str  
\  
=\> nil  
irb(main):003:0\> system(“echo #{str}”)  
  
=\> true

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)  
\
