여기 내가 작성했던 보석의 내부에 많은 방법을 시도했습니다해시 배열을 구문 분석 하시겠습니까?
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", "name"=>"Software", "type"=>"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"=>"Productivity Software", "type"=>"MarketTag"}, {"id"=>942, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/embedded-hardware-and-software", "name"=>"Embedded Hardware and Software", "type"=>"MarketTag"}, {"id"=>291, "pic"=>"/images/icons/market.png", "url"=>"https://angel.co/meeting-software", "name"=>"Meeting Software", "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"=>"Software Compliance", "type"=>"MarketTag"}]
(쿼리 당 최대 10) 예를 들어 해시입니다. 이러한 방법의
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
없음이 없습니다 :
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
내가 시도에 따라 해당 목록을 나열합니다 :
첫 번째 방법은 특별한 클래스를 만들고 해당 클래스에 각 배열 요소를지도했다 예측 가능하고 일관성 있고 실행 가능한 응답을 만들어 냈습니다.
실제 솔루션 이상으로 내 생각에 결함이 있는지에 대한 단서를 찾고 있습니다.
시도 2는 나를 위해 잘 작동하는 것 같습니다. 정확히 어떤 문제가 있습니까? BTW :'attr_accessor'는 아마도'thumbnail'이 아니라'pic'으로 불려야 할 것입니다. –
실제로 스 니펫 2가 작동합니다. 이 사건에 대한 오류를 볼 수 있습니까? – tokland
테스트 중에 오류가 너무 많습니다. 시도 2가 정확했습니다. 너희들이 나에게 말하지 않았다면 절망에서 빠져 나올 수 없었을 것이다. 감사. – cplusplusiterators