添加例外以在两个代码块之间进行选择

时间:2012-07-10 23:02:11

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我如何在两个街区之间进行选择:

page.parser.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "realviewLink", " " ))]').each do |product|

page.parser.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "gbb-productTitle", " " ))]').each do |product|

我尝试添加了begin / recuse和if / else并且不断出现意外的关键字错误。如果第一个语句= []?

,我该如何选择第二个语句

1 个答案:

答案 0 :(得分:0)

从您的代码中,更简单的方法是:

first_xpath_expr = '//*[contains(concat( " ", @class, " " ), concat( " ", "realviewLink", " " ))]'
second_xpath_expr = '//*[contains(concat( " ", @class, " " ), concat( " ", "gbb-productTitle", " " ))]'
xpath_result = page.parser.xpath(first_xpath_expr).map { |product| process_first_xpath_expr(product) }
if xpath_result.blank?
  xpath_result = page.parser.xpath(second_xpath_expr).map { |product| process_second_xpath_expr(product) }
end