RUBYのgsubでアポストロフィ[’]をエスケープ

ちょっと悩んだのでメモ

  • NG
#!/bin/env ruby -Ku
s="MAC'D"
ss=s.gsub("'","\\\'")
print ss	# MACDD
#!/bin/env ruby -Ku
s="MAC'D"
ss=s.gsub("'","\\'")
print ss	# MACDD

  • OK
#!/bin/env ruby -Ku
s="MAC'D"
ss=s.gsub("'","\\\\'")
print ss	 # MAC\'D