Leticia Esperon Leticia Esperon - 22 days ago 6
Ruby Question

Is is possible to create a string in ruby that keeps escaping characters?

I need to form a user-data script from Ruby so I need all escaping characters to remain. When I do this:

def user_data
"#!/bin/bash
sed -i -e '/<Name>loadbalanceServerIP<\/Name>/,/<Value>/s/<Value>[^<]*/<Value>1.1.1.1/' /home/wowza/conf/Server.xml"
end


The string removes the \ in the Name tag. If I put two \, it keeps them both. Any suggestion as how to keep the string the same way I wrote it?

Thank you!

Answer

You can use the %q delimiter rather than ":

def user_data
     %q[#!/bin/bash
     sed -i -e '/<Name>loadbalanceServerIP<\/Name>/,/<Value>/s/<Value>[^<]*/<Value>1.1.1.1/' /home/wowza/conf/Server.xml]
end