0

I have a CLI App that scraps 5 different deals page and save it into @@all class variable. I want all of them to have a new variable which should start from 1 to 100(because there are total 100 deals). I tried a lot but it just shows number 1 for all the deals.

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

The sample of output that I am getting is..

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,                                                                                                                                                                
: 

What I would like to have..

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,                                                                                                                                                                
: 

Any suggestions to make it work? Thank you so much in advance.

2 Answers 2

2

If you want to populate the property of an object with its position within some array:

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

That allows you to fetch, iterate, and assign in one pass with a minimal amount of mess. The each_with_index method gives you simple index for each element, and map allows you to convert that into your final array.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, this issue is solved. I got an answer. If anyone else is trying to do something like this, this is what I did.

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

I used .index method to find index number and added one to it so, it starts from 1, and assigned it to deal_info.number

1 Comment

This works but keep in mind index gets progressively slower the larger your lists get, so this ends up scaling geometrically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.