Home > front end >  How can I print double quotes in ruby
How can I print double quotes in ruby

Time:11-26

Am trying to bring output like below in AWS console for Cloudwatch metric filter pattern

[w1,w2,w3,w4=!"*10.1.1.1*"&&w5=!"*10.1.1.2*"&&w5="*admin*"]

for one of my ruby aws sdk script with below function

data_of_ips = ["10.1.1.1", "10.1.1.2"]

def run_me(data_of_ips)
    :::
    ::: 
    filter_pattern = '[w1,w2,w3,w4!='"*#{data_of_ips[0]*"'&&w4!='"*#{data_of_ips[1]*"',w5="*admin*"]'

but in the aws console output I could see the output like below without " (double quotes) for w4.

[w1,w2,w3,w4=!*10.1.1.1*&&w4=!*10.1.1.2*&&w5="*admin*"]

Please help me out to fix this.

CodePudding user response:

Is this what you expect?

data_of_ips = ["10.1.1.1", "10.1.1.2"]
def run_me(data_of_ips)
  "[w1,w2,w3,w4!=\"*#{data_of_ips[0]}*\"&&w4!=\"*#{data_of_ips[1]}*\"&&w5=\"*admin*\"]"
end

puts run_me(data_of_ips)
# => [w1,w2,w3,w4!='"*10.1.1.1*"&&w4!="*10.1.1.2*"&&w5="*admin*"]

If you want to put variable or script inside a string, I suggest you can use double quote with #{} to do that. It's more clear. And the special char like " you can just use \" to write them.

CodePudding user response:

I fixed it this way:

filter_pattern = '[w1,w2,w3,w4!="'"*#{data_of_ips[0]}*"'"&&w4!="'"*#{data_of_ips[1]}*"'"w5="*admin"]'

I used "'" (double-quotes single-quote double-quotes) and got the expected output below:

filter_pattern = '[w1,w2,w3,w4!="*10.1.1.1*"&&w4!="*10.1.1.2*",w5"*admin*"]'
  • Related