Hi Railers, I have the following code which attempts to find missing years in a list of active records. It creates a range of years for which filing documents are required. Then deletes all the years for which there are documents returning a list of years for which no documents where found. I am newbie to ruby, rails and blocks. Can someone help me write this more effectively? def outstanding_annual_return_years outstanding = [] result = formation_date.strftime("%Y").to_i result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} filed_documents.each {|d| outstanding.delete d.filing_date.strftime("%Y").to_i } outstanding end Thanks
Hi Railers, I have the following code which attempts to find missing years in a list of active records. It creates a range of years for which filing documents are required. Then deletes all the years for which there are documents returning a list of years for which no documents where found. I am newbie to ruby, rails and blocks. Can someone help me write this more effectively? def outstanding_annual_return_years outstanding = [] result = formation_date.strftime("%Y").to_i result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} filed_documents.each {|d| outstanding.delete d.filing_date.strftime("%Y").to_i } outstanding end Thanks
On Nov 9, 2005, at 3:13 PM, Leon Leslie wrote:> Hi Railers, > > I have the following code which attempts to find missing years in a > list of active records. > > It creates a range of years for which filing documents are required. > Then deletes all the years for which there are documents > returning a list of years for which no documents where found. > > I am newbie to ruby, rails and blocks. Can someone help me write this > more effectively? > > def outstanding_annual_return_years > outstanding = [] > result = formation_date.strftime("%Y").to_i > result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} > filed_documents.each {|d| > outstanding.delete d.filing_date.strftime("%Y").to_i > } > outstanding > enddef outstanding_annual_return_years (formation_date.year..Time.now.year).to_a - filed_documents.map { |f| f.filing_date.year } end What this does is take the year from the formation date and the year from today, and create a range out of them (a..b). Then, it converts the range to an array (to_a). This gives you an array of all the years between the two years, inclusive. Then it creates an array of all years for the filing dates using Array#map. This basically creates a new array, where each element of the array is replaced by the result of the associated block. So, now we have two arrays--one with all the years, and one with the years for which there are documents. Finally, we just take the difference between the two arrays, which gives you all elements in the first array that do not exist in the second array. Hope that makes sense. :) - Jamis
Hi Jamis, This was fast.. . Makes perfect sense, And works like the breeze :) Thank you.... On 11/9/05, Jamis Buck <jamis-uHoyYlH2B+GakBO8gow8eQ@public.gmane.org> wrote:> On Nov 9, 2005, at 3:13 PM, Leon Leslie wrote: > > > Hi Railers, > > > > I have the following code which attempts to find missing years in a > > list of active records. > > > > It creates a range of years for which filing documents are required. > > Then deletes all the years for which there are documents > > returning a list of years for which no documents where found. > > > > I am newbie to ruby, rails and blocks. Can someone help me write this > > more effectively? > > > > def outstanding_annual_return_years > > outstanding = [] > > result = formation_date.strftime("%Y").to_i > > result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} > > filed_documents.each {|d| > > outstanding.delete d.filing_date.strftime("%Y").to_i > > } > > outstanding > > end > > def outstanding_annual_return_years > (formation_date.year..Time.now.year).to_a - > filed_documents.map { |f| f.filing_date.year } > end > > What this does is take the year from the formation date and the year > from today, and create a range out of them (a..b). > > Then, it converts the range to an array (to_a). This gives you an > array of all the years between the two years, inclusive. > > Then it creates an array of all years for the filing dates using > Array#map. This basically creates a new array, where each element of > the array is replaced by the result of the associated block. > > So, now we have two arrays--one with all the years, and one with the > years for which there are documents. > > Finally, we just take the difference between the two arrays, which > gives you all elements in the first array that do not exist in the > second array. > > Hope that makes sense. :) > > - Jamis > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- First they laugh at you, then they ignore you, then they fight you. Then you win. -- Mahatma Karamchand Gandhi
On 11/9/05, Leon Leslie <leonleslie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi Railers, > > I have the following code which attempts to find missing years in a > list of active records. > > It creates a range of years for which filing documents are required. > Then deletes all the years for which there are documents > returning a list of years for which no documents where found. > > I am newbie to ruby, rails and blocks. Can someone help me write this > more effectively? > > def outstanding_annual_return_years > outstanding = [] > result = formation_date.strftime("%Y").to_i > result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} > filed_documents.each {|d| > outstanding.delete d.filing_date.strftime("%Y").to_i > } > outstanding > endI''ll bite. Not tested - just written into the email: | def outstanding_annual_return_years | [formation_date.year..Date.new.year] - filed_documents.map{|doc| doc.filing_date.year } | end Let me know if it works :) /Nick _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi /Nick It does not work. Two observerations [formation_date.year..Date.new.year] Returns a range instead of an array Date.new.year does not return the current year On 11/9/05, Nick Sieger <nicksieger-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 11/9/05, Leon Leslie <leonleslie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Railers, > > > > I have the following code which attempts to find missing years in a > > list of active records. > > > > It creates a range of years for which filing documents are required. > > Then deletes all the years for which there are documents > > returning a list of years for which no documents where found. > > > > I am newbie to ruby, rails and blocks. Can someone help me write this > > more effectively? > > > > def outstanding_annual_return_years > > outstanding = [] > > result = formation_date.strftime("%Y").to_i > > result.upto(Time.now.strftime("%Y").to_i) {|y| outstanding << y} > > filed_documents.each {|d| > > outstanding.delete d.filing_date.strftime ("%Y").to_i > > } > > outstanding > > end > > I''ll bite. Not tested - just written into the email: > > | def outstanding_annual_return_years > | [formation_date.year..Date.new.year] - > filed_documents.map{|doc| doc.filing_date.year } > | end > > Let me know if it works :) > /Nick > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- First they laugh at you, then they ignore you, then they fight you. Then you win. -- Mahatma Karamchand Gandhi
On 11/10/05, Leon Leslie <leonleslie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi /Nick > > It does not work. > Two observerations > [formation_date.year..Date.new.year] Returns a range instead of an array > Date.new.year does not return the current yearMy mistake, that''s what I get for not consulting the doco. Should be this: | def outstanding_annual_return_years | (formation_date.year..Date.today.year).to_a - filed_documents.map{|doc| doc.filing_date.year } | end _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails