我有以下代码创建了一个Parser
有很多parse_something
方法的类。然后最后将它们合并到parse
方法中:
class Parser
def 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
]
array_of_hashes.inject(merged_hashes,:update)
return merged_hashes
end
def parse_department
department = @data.css('#ref_2619534011')
@department_hash = {}
department.css('li').drop(1).each do | department |
department_title = department.css('.refinementLink').text
department_count = department.css('.narrowValue').text[/[\d,]+/].delete(",").to_i
@department_hash[:department] ||= {}
@department_hash[:department]["Pet Supplies"] ||= {}
@department_hash[:department]["Pet Supplies"][department_title] = department_count
end
return @department_hash
end
def parse_super_saver
(ETC...)
所以现在,我像这样使用它:
def html_pet_supplies
File.open("amazon_pet_supplies.html")
end
Parser.new.parse html_pet_supplies
但我想像这样使用它:Parser.parse html_pet_supplies
我尝试删除new
,但出现此错误:
NoMethodError:解析器的未定义方法“解析”:类
更改代码以便我这样做的最简单方法是什么Parser.parse
?