From: "Jesús Gabriel y Galán" Date: 2010-09-26T05:31:02+09:00 Subject: Re: Need assistance with hash of arrays and injecting values On Sat, Sep 25, 2010 at 2:29 PM, Alpha Blue wrote: > I'm creating a class that will run a number of tasks based on a start > and end date.  It only runs between week 4 and week 15 of my current > system. > > A current hash of arrays is defined for the current calendar year the > tasks run. > > @h = { >  :week_four => ["2010-09-20", "2010-09-26"], >  :week_five => ["2010-09-27", "2010-10-03"], >  :week_six => ["2010-10-04", "2010-10-10"], >  :week_seven => ["2010-10-11", "2010-10-17"], >  :week_eight => ["2010-10-18", "2010-10-24"], >  :week_nine => ["2010-10-25", "2010-10-31"], >  :week_ten => ["2010-11-01", "2010-11-07"], >  :week_eleven => ["2010-11-08", "2010-11-14"], >  :week_twelve => ["2010-11-15", "2010-11-21"], >  :week_thirteen => ["2010-11-22", "2010-11-28"], >  :week_fourteen => ["2010-11-29", "2010-12-05"], >  :week_fifteen => ["2010-12-06", "2010-12-12"] > } > > I want to inject these into a similar task: > > @current_week is an instance variable within the class that holds the > current week (i.e. week 10 for instance so '10') > > @weekly_report is defined as an array that will hold the report > listings. > > The VirtualReport.reportlistings method takes two dates - a start date > and an end date.  I only want them to run up to the current week.  So, > in this example, if it's week 10, it should run a report for week > 4,5,6,7,8,9, and 10.  But, it should not run a report for > 11,12,13,14,15.  Furthermore, it should check to ensure that the data is > not empty. > > for i in 0..(@current_week-4) >  @weekly_report = VirtualReport.reportlistings("INSERT HASH VALUES") > end > > Where I placed the INSERT HASH VALUES above is where I want to inject my > values based on the current week.  Therefore, I'm pretty certain the > hash needs to be ordered or sorted so that they maintain the current > week layouts.  But, I'm not very good with hashes in general and could > use some help or a better example of how to accomplish this task.  The > weekly reports will be stored in an array and used later on in the > class. > > Any help would be appreciated. If I understood correctly, you want to get for values of i 4,5,6,7,8,9,10 the arrays corresponding to the key :week_, so, first, you need to translate from the number to the word (4 => four). The easiest way would be to change your @h array, if possible: @h = { 4 => ["2010-09-20", "2010-09-26"], 5 => ["2010-09-27", "2010-10-03"], ... } If this is not possible, I would build a translation hash for that: TRANSLATION = {4 => :week_four, 5 => :week_five, ...} and do: 4.upto(@current_week) do |week| weekly_report = VirtualReport.reportlistings(@h[TRANSLATION[i]]) # do something with the report end Hope this helps, Jesus.