I’m new to working with files in Ruby, and I found the following issue.
When executing the following line:
'zip -r "c:\temp\a.zip" "c:\temp\a.txt"'
from IRB I receive the following:
=> "zip -r \"c:\\temp\\a.zip\" \"c:\\temp\\a.txt\""
and definitely file is not compressed. So my question here is:
-
Why does ruby add a wrapper
\\
around file path and
\
signs. If you could elaborate on this that would be really helpful as I wasn’t able to find info on backslashes
-
How to escape these signs?
My Environment:
Windows 7 jRuby 153
The backslashes are escapes.
In any case, if you’re just evaluating the string, that’s just the string-as-a-string. When you use single-quotes, backslashes are just backslashes, not escape characters. Internally, for a string to print a backslash, it must be escaped, so Ruby does it for you.
If you assigned it to a variable and called
puts:You’re also not actually doing anything–you’re just creating a string.
If you want to execute the string as a command, you need to be using back-ticks, not single-quotes. Also, both Ruby and Java are OS-agnostic, so you can use forward-slashes like normal people.