所以这是我正在使用的代码:
@department_hash = {}
department.css('li').each do | department |
department_title = department.css('.refinementLink').text
department_count = department.css('.narrowValue').text[/[\d,]+/]
@department_hash[:department] ||= {}
@department_hash[:department]["Pet Supplies"] ||= {}
@department_hash[:department]["Pet Supplies"][department_title] = department_count
end
产生类似这样的东西:
{:department => {“Pet Supplies”=> {“”=> nil,“Birds”=>“15,863”, “Cats”=>“243,396”,“Dogs”=>“512,965”,“Fish& Aquatic Pets”=>“46,428”, “Horses”=>“14,738”,“Insects”=>“360”,“Reptiles& 两栖动物“=>”5,843“,”小动物“=>”19,871“}}}
但我想要的是产生这样的东西:
{:department => {“Pet Supplies”=> [“”=> nil,“Birds”=>“15,863”, “Cats”=>“243,396”,“Dogs”=>“512,965”,“Fish& Aquatic Pets”=>“46,428”, “Horses”=>“14,738”,“Insects”=>“360”,“Reptiles& 两栖动物“=>”5,843“,”小动物“=>”19,871“]}}
我如何做到这一点?
修改
这个怎么样?这是有效的红宝石代码吗?
departments: { "Pet Supplies": [ "Birds" : 16651, "Cats" : 242910, etc ] }
答案 0 :(得分:1)
你不能。
你能做的最好的事情就是这样:
{ foo: [ { key: :value }, { key: :value } ] }
这样做:
@department_hash = {}
department.css('li').each do | department |
department_title = department.css('.refinementLink').text
department_count = department.css('.narrowValue').text[/[\d,]+/]
@department_hash[:department] ||= {}
@department_hash[:department]["Pet Supplies"] ||= []
@department_hash[:department]["Pet Supplies"] << { key: :value } # replace with your own hash
end
end