Reputation: 165
\I have the following path: E:\Path\Reports.
If i want to store this path in a variable in ruby, i need to use "\\":
$location = "E:\\Path\\Reports"
Is there a way to avoid using the "\\" in the variable and still have the right path?
Upvotes: 0
Views: 141
Reputation: 1811
An easier way would be to use File.join
. It'll use /
but Ruby will take care of converting that to \
under the hood on Windows.
$location = File.join('E', 'Path', 'Reports')
# $location is actually 'E/Path/Reports' now,
# but Ruby knows to convert the '/' to '\' on Windows.
This also has the benefit of making your code OS-independent.
Upvotes: 1
Reputation: 14498
You can use the %q
:
$location = %q{E:\Path\Reports}
=> $location
"E:\\Path\\Reports"
=> puts $location
E:\Path\Reports
Upvotes: 1
Reputation: 16738
if you use this
location = 'E:\path\Reports'
it will be automatically escaped for you
Upvotes: 1
Reputation: 12558
Single-quoted strings interpret \
as a literal character, rather than an escape character (except for \'
, which is '
literally):
$location = 'E:\Path\Reports'
Upvotes: 0