【问题标题】:How do I mass-assign unique instance variable names when iterating and parsing over an array of hashes?迭代和解析哈希数组时,如何批量分配唯一的实例变量名称?
【发布时间】:2012-07-09 22:42:25
【问题描述】:

这是我开始的大哈希(实际上它已经改进了一两步,但这是我现在开始的内容。

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

【问题讨论】:

  • 你打算如何处理所有这些动态创建的实例变量?
  • 我将在我为课堂制作的 ruby​​-on-rails 练习应用程序中使用它们。这是我将要制作的宝石的一部分,这是我 /lib 文件夹中的“公司”类。
  • 这个大哈希是来自 AngelList.co API 的已解析 http 响应

标签: ruby arrays hash iterator mass-assignment


【解决方案1】:

这是您问题的直接答案:您可以通过调用 instance_variable_set 来定义任意实例变量。

@markets_array.each_with_index do |market, i|
  market.each do |k, v|
    instance_variable_set "market#{i}_#{k}", v

    # this will define @market0_id = 448  
  end
end

【讨论】:

  • 哇,好复杂。幸运的是,这不是我第一次看到这种操作。谢谢。数组内部的哈希很棘手。
  • 有什么复杂的? instance_variable_set?一点都不。 :)
  • 我想让我感到困惑的是你的块参数名称,尤其是 k 和 v。抱歉,我有点超出了我的舒适区,我并没有要求你为我做这项工作,但是市场['id'] 是如何以及在哪里被调用的呢?
  • 当您在哈希上调用#each 并提供一个块时,each 将两个参数传递给一个块,键和值(k 和 v,为简洁起见)。因为,您知道,哈希是键/值结构。对于市场,ks 之一将是“id”,其对应的 v 将是 448。
猜你喜欢
  • 2010-12-09
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多