Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Backslash conundrum
# Needs to escape out backslashes and other characters
# Has to save a file with all characters escaped out like
# \% \[ \]  \\  etc

>> a = "hello, this is a \ backslash in a string"
>> a = a.gsub(/([^A-Za-z0-9 ])/) { |match| '\\' + match }
=> "hello\\, this is a  backslash in a string"
>> afile = File.new('test1.txt', "w")
=> #<File:test1.txt>
>> afile.puts(a)
=> nil
>> afile.close  
=> nil


Output of test file:
hello\, this is a  backslash in a string