tags:

views:

58

answers:

2

Hi All,

I'm new to ruby and came across an interesting problem yesterday.

I was parsing a file and some lines of the file ended with "\". I wanted to use gsub to find and replace it. I tried '\' and /\/ and neither one correctly matched "\".

I ended up getting around it by using a combination of chop and strip but it left me thinking how would I do this if I ever need to again?

Anyone know how I might solve this problem?

Thanks

+3  A: 

You need to escape the escape sign as well. So this should work:

/\\/
Gumbo
+1  A: 

Passing a string to gsub that will then be compiled to a regex:

"abc\def".gsub("\\", "")
=> "abcdef"

Or just providing the regex directly:

"abc\def".gsub(/\\/, "")
=> "abcdef"
jhickner