Escaping Single Quotes with Ruby
Dec 31
I ran into a problem this evening when inserting data in MySQL. Some of the data had single quotes and I needed to escape them. One easy one to do this is using the following regular expression:
gsub(/[']/, ‘\\\\\”)
A simple example to run is:
puts “My name’s Dennis”.gsub(/[']/, ‘\\\\\”)
I know there are a couple of methods within the Ruby MySQL module, but I had problems and settled for the example listed above.
