首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同样的,如果语句对一种情况有效,但对于另一种类似的情况,则失败?

同样的,如果语句对一种情况有效,但对于另一种类似的情况,则失败?
EN

Stack Overflow用户
提问于 2013-08-22 10:17:09
回答 1查看 48关注 0票数 0

我有这样的代码:

代码语言:javascript
复制
require 'nokogiri'

class Parser
  def self.parse(html)
    @data = Nokogiri.HTML(open(html))
    merged_hashes = {}

    array_of_hashes = [
      parse_department,
      parse_super_saver,
      parse_new_arrivals,
      parse_out_of_stock,
      parse_categories,
      parse_results,
      parse_category
    ]
    array_of_hashes.inject(merged_hashes,:update)

    return merged_hashes
  end

  ## Categories

  (etc...)  

  def self.parse_results
    results = @data.css('#refinements ul').first
    unless results
      @results_hash = {}
      return @results_hash
    end

    if results.css('li:nth-child(1) a span').text == "Pet Supplies"
      @results_hash = {}
      @results_hash[:results] ||= {}
      @results_hash[:results] = @data.at_css('#resultCount span').text[/(\S+) Results$/i, 1].delete(",").to_i
    else
      @results_hash = {}
    end

    return @results_hash
  end

  ## Hot Lists

  def self.parse_category
    category = @data.at_css('#zg_listTitle span')

    unless category
      @category_hash = {}
      return @category_hash
    end

    @category_hash = {}
    @category_hash[:category] ||= {}
    @category_hash[:category] = @data.at_css('#zg_listTitle span').text

    return @category_hash
  end
end

这样做没问题:

代码语言:javascript
复制
    results = @data.css('#refinements ul').first
    unless results
      @results_hash = {}
      return @results_hash
    end

当没有#refinements ul元素时,代码停止,返回一个空的灰。

但如果是.

代码语言:javascript
复制
    category = @data.at_css('#zg_listTitle span')
    unless category
      @category_hash = {}
      return @category_hash
    end

即使没有任何#zg_listTitle span元素,代码似乎仍在继续。

有什么问题吗?

编辑:

Rspec:

代码语言:javascript
复制
require File.dirname(__FILE__) + '/parser.rb'

def html_pet_supplies
  File.open("parse_categories/amazon_pet_supplies.html")
end

def html_dog_supplies
  File.open("parse_categories/amazon_dog_supplies.html")
end

def html_bird_supplies
  File.open("parse_categories/amazon_bird_supplies.html")
end

def html_baby
  File.open("parse_hotlists/amazon_baby.html")
end

(etc.)

describe "Results (Dogs)" do
  let(:results_hash) { Parser.parse html_dog_supplies } 

  it "should return correct hash" do
    expect(results_hash[:results]).to eq(514265)
  end
end


## Hot Lists

describe "Category" do
  let(:category_hash) { Parser.parse html_baby } 

  it "should return correct hash" do
    expect(category_hash[:category]).to eq("Baby")
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-22 10:21:20

如果没有找到#zg_listTitle span@data.at_css('#zg_listTitle span')将返回nil。因此,我认为您需要使用if而不是unless

代码语言:javascript
复制
category = @data.at_css('#zg_listTitle span')
if category
  @category_hash = {}
  return @category_hash
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18377686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档