ruby - How to use variable in Nokogiri contains -


i have xml file like:

<main>   <key>xxx</key>   <key>yyy</b>   <key>zzz</key> </main> 

to check whether xml has node text "yyy", can use:

xml = nokogiri::xml(file) if xml.at('main/key:contains("yyy")')   #code go here end 

but if text value variable how can use same statement? note: have tried below solution , doesn't work:

var = "yyy" if xml.at("main/key:contains(#{var})") 

"main/key:contains(#{var})" 

interpolates to

"main/key:contains(yyy)" 

note absence of quotes. want this:

"main/key:contains(\"#{var}\")" 

or more prettily

%q{main/key:contains("#{var}")} 

and clever escaping if not sure content of var.


Comments