Forum: Ruby question to write \4 into file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cheyne L. (Guest)
on 2009-05-09 22:46
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.
7stud -. (Guest)
on 2009-05-09 23:56
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?
7stud -. (Guest)
on 2009-05-09 23:58
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.
Cheyne L. (Guest)
on 2009-05-11 23:47
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
Rob B. (Guest)
on 2009-05-12 00:25
(Received via mailing list)
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
removed_email_address@domain.invalid
Brian C. (Guest)
on 2009-05-12 00:25
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
Cheyne L. (Guest)
on 2009-05-12 02:04
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...
Brian C. (Guest)
on 2009-05-12 11:37
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)
\\
This topic is locked and can not be replied to.