报价问题(红宝石)

时间:2010-01-27 02:51:58

标签: ruby syntax quotes double-quotes syntax-checking

任何想法如何将正确的参数传递给xpath?必须有一些关于如何使用单/双引号的东西。当我使用变量

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'"给了我一个不正确的值或

parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'"给我一个错误语法错误,意外的tIDENTIFIER,期待$ end

require 'rubygems'
require 'mechanize'

parser_xpath_identificator = "'//table/tbody[@id=\"threadbits_forum_251\"]/tr'"
#   parser_xpath_identificator = "'//table/tbody[@id="threadbits_forum_251"]/tr'"
    #gives an error: syntax error, unexpected tIDENTIFIER, expecting $end

agent = WWW::Mechanize.new
page = agent.get("http://www.vbulletin.org/forum/index.php")
page = page.link_with(:text=>'vB4 General Discussions').click
puts "Page title: #{page.title}"
puts "\nfrom variable: #{page.parser.xpath(parser_xpath_identificator).length}"
puts "directly: #{page.parser.xpath('//table/tbody[@id="threadbits_forum_251"]/tr').length}"

1 个答案:

答案 0 :(得分:2)

在这两种情况下,您都将单引号直接嵌套在双引号内,我认为这是不正确的。试试这个:

parser_xpath_identificator = '//table/tbody[@id="threadbits_forum_251"]/tr'