这是我开始的大哈希(实际上它已经改进了一两步,但这是我现在开始的。
angel_hash = {"follower_count"=>1369, "name"=>"AngelList", "markets"=>
[{"display_name"=>"Startups", "name"=>"startups", "id"=>448, "tag_type"=>"MarketTag",
"angellist_url"=>"http://angel.co/startups-1"}, {"display_name"=>"Venture Capital",
"name"=>"venture capital", "id"=>856, "tag_type"=>"MarketTag",
"angellist_url"=>"http://angel.co/venture-capital"}], "video_url"=>"",
"created_at"=>"2011-03-18T00:24:29Z", "updated_at"=>"2012-07-09T14:12:28Z",
"product_desc"=>"AngelList is a platform for startups to meet investors and talent. ",
"blog_url"=>"http://blog.angel.co",
"thumb_url"=>"https://s3.amazonaws.com/photos.angel.co/startups/i/6702-
766d1ce00c99ce9a5cbc19d0c87a436e-thumb_jpg.jpg", "id"=>6702,
"company_url"=>"http://angel.co", "locations"=>[{"display_name"=>"San Francisco",
"name"=>"san francisco", "id"=>1692, "tag_type"=>"LocationTag",
"angellist_url"=>"http://angel.co/san-francisco"}], "community_profile"=>false, "status"=>
{"message"=>"Done Deal: @volunteerspot raises $1.5M
http://techcrunch.com/2012/06/27/targeting-power-moms-volunteerspot-secures-1-5m-in-
series-a-from-ff-venture-capital-and-more/ \316\207 20 intros on AngelList \316\207 Funded
by @ff-venture-capital", "created_at"=>"2012-06-28T20:37:58Z", "id"=>63110},
"twitter_url"=>"http://twitter.com/angellist", "high_concept"=>"A platform for startups",
"logo_url"=>"https://s3.amazonaws.com/photos.angel.co/startups/i/6702
-766d1ce00c99ce9a5cbc19d0c87a436e-medium_jpg.jpg",
"angellist_url"=>"http://angel.co/angellist", "screenshots"=>
[{"thumb"=>"https://s3.amazonaws.com/screenshots.angel.co/98/6702/009cff275fb96709c915c4d4abc9
43d6-thumb_jpg.jpg",
"original"=>"https://s3.amazonaws.com/screenshots.angel.co/98/6702/009cff275fb96709c915c4d4abc
943d6-original.jpg"}], "hidden"=>false}
从这个哈希中,我解析了一些元素,并且在遇到嵌入式数组之前做得很好
module SimpleAngel
class Company
attr_accessor :followers, :company_name, :markets_array, :date_joined, :locations_array
attr_accessor :high_concept, :high_concept_long, :thumbnail_logo, :full_size_logo
attr_accessor :angel_url, :twitter_url, :company_url, :blog_url
def initialize(angel_hash)
@followers = angel_hash['follower_count']
@company_name = angel_hash['name']
@markets_array = angel_hash['markets']
@markets_array.each_with_index do |market, i|
###This is where I'm stuck. I want to pull out individual elements
# from each array AND dynamically assign unique instance variable names for
# each separate market in the markets array. Something like @market1_name,
# @market1_id, etc.
end
@date_joined = angel_hash['created_at']
@locations_array = angel_hash['locations']
@high_concept = angel_hash['high_concept']
@high_concept_long = angel_hash['product_desc']
@thumbnail_logo = angel_hash['thumb_url']
@full_size_logo = angel_hash['logo_url']
@angel_url = angel_hash['angellist_url']
@twitter_url = angel_hash['twitter_url']
@company_url = angel_hash['company_url']
@blog_url = angel_hash['blog_url']
end
end
end