From: Rob Biedenharn Date: 2009-05-12T05:24:08+09:00 Subject: Re: question to write \4 into file On May 11, 2009, at 3:47 PM, Cheyne Li wrote: > 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 > -- 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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com