Advertisement
Guest User

EscapingRuby

a guest
Feb 8th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.29 KB | None | 0 0
  1. ESCAPE_MAP   =   { '"' => '\\"', "'" => "\\'" }
  2.  
  3. def escape_quotes(str)
  4.   if str
  5.     result = str.gsub(/(["'])/u) {|match| ESCAPE_MAP[match] }
  6.  else
  7.    ''
  8.  end
  9. end
  10.  
  11. string = 'double-quotes "escaped"'
  12. escapedStr = escape_quotes(string)
  13. puts escapedStr
  14. => double-quotes \'escaped\'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement