Ruby on rails 如何将Heredocs与XPath结合使用

Ruby on rails 如何将Heredocs与XPath结合使用,ruby-on-rails,ruby,xpath,heredoc,Ruby On Rails,Ruby,Xpath,Heredoc,我面临以下问题: 有这样一个XPath(您可以很长时间看到): 现在,为了让它更具可读性,我尝试使用herdoc将其拆分为多行: xpath = <<-XPath.gsub(/\s+/,'') //h2[contains(text(),"Kontakt")]/.. /following-sibling::div[contains(@class,"body") and child::dl[contains(@class,"horizontal")]] XPath xpat

我面临以下问题:

有这样一个XPath(您可以很长时间看到):

现在,为了让它更具可读性,我尝试使用herdoc将其拆分为多行:

xpath = <<-XPath.gsub(/\s+/,'')
  //h2[contains(text(),"Kontakt")]/..
  /following-sibling::div[contains(@class,"body") and
  child::dl[contains(@class,"horizontal")]]
XPath

xpath=您尝试处理相同的情况(第1个)→二线和二线→第三个字符串)以不同的方式:在第一种情况下连接字符串,在后一种情况下插入空格。需要一个AI来理解您的意思:)

我建议你不要在空间位置折断绳子。在这种情况下,
/\s*\n\s*|\a\s+|\s+\Z/m
regexp(删除包含空格、开头和结尾空格的回车符)将起到以下作用:

xpath = <<-XPath.gsub(/\s*\n\s*|\A\s+|\s+\Z/m,'')
  //h2[contains(text(),"Kontakt")]/..
  /following-sibling::div[contains(@class,"body") and child
  ::dl[contains(@class,"horizontal")]]
XPath

# "//h2[contains(text(),\"Kontakt\")]/../following-sibling::div[contains(@class,\"body\") and child::dl[contains(@class,\"horizontal\")]]"

xpath=感谢您的帮助:)
xpath = <<-XPath.gsub(/\s*\n\s*|\A\s+|\s+\Z/m,'')
  //h2[contains(text(),"Kontakt")]/..
  /following-sibling::div[contains(@class,"body") and child
  ::dl[contains(@class,"horizontal")]]
XPath

# "//h2[contains(text(),\"Kontakt\")]/../following-sibling::div[contains(@class,\"body\") and child::dl[contains(@class,\"horizontal\")]]"