ruby - How do I mass-assign unique instance variable names when iterating and parsing over an array of hashes? -
Here is the big hash that I started (actually, one or two steps have been refined but I started it I'm at this point.
angel_hash = { "follower_count" => 1369, "name" = & gt; "Anjelilist", "market" = & gt; [[ "display_name" = & gt; "startup", "name" = & gt; "startup", "id" = & gt; 448, "tag type" = & gt; "MarketTag", "angellist_url" = & gt; "http: / /angel.co/startups-1 "}," "Display_name" = & gt; "venture capital", "name" = & gt; "venture capital", "id" = & gt; 856, "tag_type" = & Gt; "MarketTag", "angellist_url" = & gt; "Http://angel.co/ventur E-capital "}]," video_url "=> gt;" "," created_at "=>
" 2011-03-18T00: 24: 29Z "," updated_at = "=" 2012-07-09T14: 12: 28Z "," product_desc "= & gt;" Anjelilist investors and is a platform to meet the talent. "," Blog_url "= & gt;" http://blog.angel.co "," thumb_url " = & gt; "https://s3.amazonaws.com/photos.angel.co/startups/i/6702- 766d1ce00c99ce9a5cbc19d0c87a436e-thumb_jpg.jpg", "id" = & gt; 6702, "company_url" = & gt; "Http://angel.co", "Location" = & gt; [["Display_name" = & gt; "San Francisco", "Name" = & gt; "San Francisco", "id" = & gt; 1692, "Tag Type" => gt; "LocationTag", "angellist_url" = & gt; "http://angel.co/san-francisco"}, "community_profile" = & gt; Wrong, "status" = & gt; {"Message" = & gt; "Done Deal: @ Volnthierspot is $ 1.5 M arises http://techcrunch.com/2012/06/27/targeting-power-moms-volunteerspot-secures-1-5m-in- series a- Anjelilist \ 316 \ 207 "FF-venture-capital-and-more / \ 316 \ 207 20 introduction," "created" => = F.F.-venture capital-funded "2012-06-28 T-20: 37: 58S" , "Id" => 63110}, "twitter_url" => " Http://twitter.com/angellist "," high_concept "=>" A forum for startup "," logo_url "=> Https: //s3kamzonoskcom/fotoj.anjelkko/startups/i/6702 -766d1ce00c99ce9a5cbc19d0c87a436e-Mdyam_jpgkjpg "," Angelist_uarel "= & gt;" http://angel.co/angellist "" screenshot "= & gt; [{" thumb "= & gt;" https://s3.amazonaws.com/screenshots.angel.co/98/6702/009cff275fb96709c915c4d4abc9 43d6- Thumb_jpg.jpg "," original "= & gt;" https: // S3. amazonaws.com/screenshots.angel.co/98/6702/009cff275fb96709c915c4d4abc 943d6-original.jpg "}]," hidden "= & gt; false} I parse out the hash some elements, and right when I'm running in embedded arrays
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, start def at BLOG_URL (angel_hash) @followers = angel_hash [ 'FOLLOWER_COUNT'] @company_name = angel_hash [ 'name'] @markets_array = angel_hash [ 'market "] @market_re.each_v_indexcourse | Market, I. | ### This is where I am stuck. I want to remove different elements from each array # and dynamically assign variable names to unique names for each separate market in the market array. in. @ Market1_name, # @ 'some @ end_joined = angel_hash [' created_at like market1_id, etc.] @locations_array = angel_hash [ 'location'] @ Hai_konis = Anjil_has [ 'Uchc_kon cept'] @ Hai_consept_ Long = Anjil_has [ 'Utpad_ DCC'] @ Thumbnail_logo [angel_hash] ['thumb_url']] end of a Here is a direct answer to your question: You can define arbitrariness. Example variable by calling instance_variable_set @ market_array.each_with_index do | Market, i | Market.each do | K, v | Instance_variable_set "Market # {i} _ # {k}", V # will define @ market0_id = 448 end and
Comments
Post a Comment