2017-04-08 119 views
0

我有一个CLI应用程序,它将5个不同的交易页面分割并保存到@@所有类变量中。我希望他们都有一个新的变量,应该从1到100(因为总共有100个交易)。我尝试了很多,但它只显示了所有交易的第一名。遍历所有数组元素并将变量添加到元素ruby

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

如果你想填充一个对象的属性与它在一些阵列中的位置:

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

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

+0

它也工作!非常感谢! –

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

+0

这是有效的,但请记住'index'越来越慢,列表越大,所以这最终会以几何形式进行缩放。 – tadman