0

这是一个示例哈希(每个查询最多 10 个)

parsed_response = [{"id"=>3, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/mobile-4", "name"= >"Mobile", "type"=>"MarketTag"}, {"id"=>12, "pic"=>"/images/icons/market.png", "url"=>"https://angel .co/enterprise-software", "name"=>"Enterprise Software", "type"=>"MarketTag"}, {"id"=>10, "pic"=>"/images/icons/market.png ", "url"=>"https://angel.co/saas", "name"=>"SaaS", "type"=>"MarketTag"}, {"id"=>841, "pic"= >"/images/icons/market.png", "url"=>"https://angel.co/software","名称"=>"软件", "类型"=>"MarketTag"}, {"id"=>263, "pic"=>"/images/icons/market.png", "url"=>"https ://angel.co/restaurants-2", "name"=>"Restaurants", "type"=>"MarketTag"}, {"id"=>376, "pic"=>"/images/icons/ market.png", "url"=>"https://angel.co/productivity-software", "name"=>"生产力软件", "type"=>"MarketTag"}, {"id"=> 942, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/embedded-hardware-and-software", "name"=>"嵌入式硬件和软件”,“类型”=>“市场标签”},{"id"=>291, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/meeting-software", "name"=>"会议软件", "type"=>"MarketTag"}, {"id"=>13764, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co /hardware-software", "name"=>"Hardware + Software", "type"=>"MarketTag"}, {"id"=>2983, "pic"=>"/images/icons/market.png" , "url"=>"https://angel.co/software-compliance", "name"=>"软件合规性", "type"=>"MarketTag"}]https://angel.co/meeting-software", "name"=>"会议软件", "type"=>"MarketTag"}, {"id"=>13764, "pic"=>"/images/ icons/market.png", "url"=>"https://angel.co/hardware-software", "name"=>"硬件 + 软件", "type"=>"MarketTag"}, {"id "=>2983, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software-compliance", "name"=>"软件合规性", “类型”=>“市场标签”}]https://angel.co/meeting-software", "name"=>"会议软件", "type"=>"MarketTag"}, {"id"=>13764, "pic"=>"/images/ icons/market.png", "url"=>"https://angel.co/hardware-software", "name"=>"硬件 + 软件", "type"=>"MarketTag"}, {"id "=>2983, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software-compliance", "name"=>"软件合规性", “类型”=>“市场标签”}], "url"=>"https://angel.co/hardware-software", "name"=>"硬件+软件", "type"=>"MarketTag"}, {"id"=>2983, " pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software-compliance", "name"=>"软件合规性", "type"=>"市场标签"}], "url"=>"https://angel.co/hardware-software", "name"=>"硬件+软件", "type"=>"MarketTag"}, {"id"=>2983, " pic"=>"/images/icons/market.png", "url"=>"https://angel.co/software-compliance", "name"=>"软件合规性", "type"=>"市场标签"}]

我在我创建的 gem 中尝试了很多方法。

第一种方法是创建一个特殊的类并将每个数组元素映射到该类:

class IDSearch
  attr_accessor :id, :thumbnail, :url, :name, :type
  def initialize(the_hash)
    @id   = the_hash['id']
    @url  = the_hash['url']
    @pic  = the_hash['pic']
    @name = the_hash['name']
    @type = the_hash['type']
  end
end

我将根据尝试列出它们:

Attempt 1:
IDSearch.new(parsed_response)

Attempt 2:
parsed_response.map {|t| IDSearch.new(t)}

Attempt 3:
parsed_response.each do |hsh|
SimpleAngel::IDSearch.new(hsh)
end

Attempt 4:
10.times do |i|
  instance_variable_set "@response_#{i}", parsed_response[i]        
end

Attempt 5 (getting desperate)
@response0 = parsed_response[0]
@response1 = parsed_response[1]
@response2 = parsed_response[2]
@response3 = parsed_response[3]
@response4 = parsed_response[4]
@response5 = parsed_response[5]
@response6 = parsed_response[6]
@response7 = parsed_response[7]
@response8 = parsed_response[8]
@response9 = parsed_response[9]

Attempt6: (this time inside of a rails controller)

search_object = SimpleAngel::Search.new
responses = search_object.id_search(params[:query], params[:type])
responses.each_with_index do |response, i|
  u      = Idquery.new
  u.name = response[i]['name']
  u.id   = response[i]['id']
  u.url  = response[i]['url']
  u.type = response[i]['type']
  u.pic  = response[i]['pic']
  u.save
end

这些方法都没有产生可预测的、连贯的、可行的反应。

不仅仅是一个实际的解决方案,我正在寻找关于我的想法如何存在缺陷的线索。

4

2 回答 2

0

在 Attempt1 中,您期望 中的散列IDSearch.new并实际传递一个数组。如果您改为:

parsed_response.each {|hash| p IDSearch.new(hash)}

它工作得很好。

@id=3, @url="https://angel.co/mobile-4", @pic="/images/icons/market.png", @name="Mobile", @type="MarketTag"
于 2012-07-14T10:17:21.837 回答
0
require 'ostruct'
parsed_response.map {|resp| OpenStruct.new(resp)}

消除了对特定类的需求。

于 2012-07-14T21:34:29.637 回答