Using gsub to replace double slashes in Ruby
I am having some issues using gsub to replace double slashes. The problem is this:
I built a small script to parse YAML files for a directory location, and then to use that to glob the files in that directory. Say this is the directory it finds:
C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Clas开发者_JAVA技巧ses
This is a Windows directory, and the backslashes are escaped by YAML parser, so this really gets loaded from parser as:
C:\\Program Files\\Adobe\\Adobe Flash CS3\\en\\Configuration\\ActionScript 3.0\\Classes
To use this directory, I wanted to gsub away these double slashes:
path.gsub('\\','/')
This call replaced most of the double backslashes in the path, though the script still did not work. When I looked at what the path had become, I found that there was still one backslash that had not been replaced by gsub:
\en
What explains this strange behavior?
Seems to work fine on my end.
irb(main):001:0> string = "C:\\Program Files\\Adobe\\Adobe Flash CS3\\en\\Configuration\\ActionScript 3.0\\Classes"
=> "C:\\Program Files\\Adobe\\Adobe Flash CS3\\en\\Configuration\\ActionScript 3.0\\Classes\n"
irb(main):003:0> string.gsub('\\', '/')
=> "C:/Program Files/Adobe/Adobe Flash CS3/en/Configuration/ActionScript 3.0/Classes\n"
What version of Ruby are you using? Perhaps something else is going on in the script? Because it works fine in irb for me.
精彩评论