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:
我使用nokogiri css已经有一段时间了,我希望能够在css选择器中使用ruby表达式插值,但它不起作用。这是我想使用的代码:
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?
有什么方法可以让类似的语法起作用吗?
1 个解决方案
#1
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:
它与Nokogiri没有关系:问题是您使用的是单引号,但是Ruby中的字符串插入需要双引号。由于在CSS选择器中也允许使用单引号,我将写:
doc.css("span[class='#{k}']").children
#1
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:
它与Nokogiri没有关系:问题是您使用的是单引号,但是Ruby中的字符串插入需要双引号。由于在CSS选择器中也允许使用单引号,我将写:
doc.css("span[class='#{k}']").children