One liner please

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 L. wrote:

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. :slight_smile:

  • Jamis

Hi Jamis,

This was fast… . Makes perfect sense, And works like the breeze :slight_smile:

Thank you…

On 11/9/05, Jamis B. [email protected] wrote:

outstanding
Then, it converts the range to an array (to_a). This gives you an
gives you all elements in the first array that do not exist in the


First they laugh at you, then they ignore you, then they fight you.
Then you win.
– Mahatma Karamchand Gandhi