2

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?

fartagaintuxedo
  • 749
  • 10
  • 28

1 Answers1

8

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:

doc.css("span[class='#{k}']").children
Community
  • 1
  • 1
tokland
  • 66,169
  • 13
  • 144
  • 170