I have been using nokogiri css for a while and i would like to be able to use ruby expression interpolation inside css selectors but it doesn’t work. This is the code i would like to use:
doc = Nokogiri::HTML(open('http://www.somepage.com'))
keys=["BHiuG", "hUYtb4F", "jefHUY78i"]
keys.each do |k|
keyvalue = doc.css('span[class="#{k}"]').children
puts keyvalue
end
Is there any way to get a similar syntax working?
It has nothing to do with Nokogiri: the problem is that you are using single quotes but string interpolation in Ruby requires double quotes. Since single quotes are also allowed on CSS selectors, I’d write: