迭代所有数组元素并向元素ruby添加变量

时间:2017-04-08 19:53:21

标签: arrays ruby loops enumerator

我有一个CLI应用程序,可以删除5个不同的交易页面并将其保存到@@ all类变量中。我希望他们所有人都有一个新变量,应该从1到100开始(因为总共有100个交易)。我尝试了很多,但它只显示了所有交易的第一。

def deals_listing
  all_deals = PopularDeals::NewDeals.all
  @deals = []
  all_deals.collect do |deal_info|
    i = 1
    deal_info.number = i
    @deals << deal_info
    i = i + 1
  end
  @deals
  binding.pry
 end

我得到的输出样本是..

pry(#<PopularDeals::CLI>)> @deals                                                                                                                                       
=> [#<PopularDeals::NewDeals:0x00000001aaf220                                                                                                                               
  @deal_rating="+7",                                                                                                                                                        
  @number=1,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$7.64",                                                                                                                                                           
  @title=                                                                                                                                                                   
   "Back Again at Amazon Campbell's Slow Cooker Sauces, Apple Bourbon Pulled Pork, 13 Ounce (Pack of 6) as low as $7.64 w/ Subscribe and Save S&S w/ Free Shipping",        
  @url=                                                                                                                                                                     
   "https://slickdeals.net/f/10033448-back-again-at-amazon-campbell-s-slow-cooker-sauces-apple-bourbon-pulled-pork-13-ounce-pack-of-6-as-low-as-7-64-w-subscribe-and-save-s-
s-w-free-shipping">,                                                                                                                                                        
 #<PopularDeals::NewDeals:0x00000001a876f8                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=1,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$5.33",                                                                                                                                                           
  @title=                                                                                                                                                                   
   "LUCKLED 20 LED Solar Powered Dragonfly String Lights Multi-color $5.33 AC, FS w/prime @Amazon",                                                                         
  @url=                                                                                                                                                                     
ing-lights-multi-color-5-33-ac-fs-w-prime-amazon">,                                                                                                                         
 #<PopularDeals::NewDeals:0x00000001a84228                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=1,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$339.99",                                                                                                                                                         
  @title=                                                                                                                                                                   
ping @ Walmart",                                                                                                                                                            
  @url=                                                                                                                                                                     
efurbished-339-99-free-shipping-walmart">,                                                                                                                                  
 #<PopularDeals::NewDeals:0x00000001a80ad8                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=1,                                                                                                                                                                
: 

我想拥有什么......

pry(#<PopularDeals::CLI>)> @deals                                                                                                                                       
=> [#<PopularDeals::NewDeals:0x00000001aaf220                                                                                                                               
  @deal_rating="+7",                                                                                                                                                        
  @number=1,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$7.64",                                                                                                                                                           
  @title=                                                                                                                                                                   
   "Back Again at Amazon Campbell's Slow Cooker Sauces, Apple Bourbon Pulled Pork, 13 Ounce (Pack of 6) as low as $7.64 w/ Subscribe and Save S&S w/ Free Shipping",        
  @url=                                                                                                                                                                     
   "https://slickdeals.net/f/10033448-back-again-at-amazon-campbell-s-slow-cooker-sauces-apple-bourbon-pulled-pork-13-ounce-pack-of-6-as-low-as-7-64-w-subscribe-and-save-s-
s-w-free-shipping">,                                                                                                                                                        
 #<PopularDeals::NewDeals:0x00000001a876f8                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=2,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$5.33",                                                                                                                                                           
  @title=                                                                                                                                                                   
   "LUCKLED 20 LED Solar Powered Dragonfly String Lights Multi-color $5.33 AC, FS w/prime @Amazon",                                                                         
  @url=                                                                                                                                                                     
ing-lights-multi-color-5-33-ac-fs-w-prime-amazon">,                                                                                                                         
 #<PopularDeals::NewDeals:0x00000001a84228                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=3,                                                                                                                                                                
  @posted="Posted Today",                                                                                                                                                   
  @price="$339.99",                                                                                                                                                         
  @title=                                                                                                                                                                   
ping @ Walmart",                                                                                                                                                            
  @url=                                                                                                                                                                     
efurbished-339-99-free-shipping-walmart">,                                                                                                                                  
 #<PopularDeals::NewDeals:0x00000001a80ad8                                                                                                                                  
  @deal_rating="+6",                                                                                                                                                        
  @number=4,                                                                                                                                                                
: 

有什么建议可以让它发挥作用吗?非常感谢你。

2 个答案:

答案 0 :(得分:2)

如果要填充对象的属性,并将其位置放在某个数组中:

@deals = PopularDeals::NewDeals.all.each_with_index.map do |deal, i|
  deal.number = i
  deal
end

这允许您在一次通过中获取,迭代和分配最少量的混乱。 each_with_index方法为每个元素提供了简单的索引,map允许您将其转换为最终数组。

答案 1 :(得分:0)

嗯,这个问题已经解决了。我得到了答案。如果其他人试图做这样的事情,这就是我所做的。

def deals
    all_deals = PopularDeals::NewDeals.all
    @deals = []
    all_deals.collect do |deal_info|
      deal_info.number = all_deals.index(deal_info).to_i + 1
      @deals << deal_info
    end
    @deals
  end

我使用.index方法查找索引号并添加一个索引号,因此,它从1开始,并将其分配给deal_info.number