tags:

views:

59

answers:

1
 lotofxpath = arrayofmanyxpaths.map{|s| "\"" + s + "\""}.join(",")
 puts lotofxpath #=> "/html/body/a[1]", "/html/body/a[2]"

 newb = doc.xpath(lotofxpath).to_a

this will not work, and complain about invalid xpath.

however, copying pasting the output string

 newb = doc.xpath("/html/body/a[1]", "/html/body/a[2]").to_a

will work without problems!!!

what is happening here ?

+1  A: 

In the first case you end up calling Nokogiri as follows

newb = doc.xpath("\"/html/body/a[1]\", \"/html/body/a[2]\"").to_a

and this is not the right Ruby syntax to accomplish what you are trying to do. The right way is

newb = doc.xpath(*arrayofmanyxpaths).to_a
Simone Carletti