Alex Rivera | Logout

Iterating over days in Ruby

Asked 2011-07-07T18:29:31.540
9

I've tried the following:

def next_seven_days
  today = Date.today
  (today .. today + 7).each { |date| puts date } 
end 

But this just gives me the first and last date. I can't figure out how to get all the ones in between.

I was trying to follow the example here: http://www.whynotwiki.com/Ruby_/_Dates_and_times

Edit
Report

1 Answer

2

Your function RETURNS an enumeration designated by 2011-07-07..2011-07-14 which is displayed in your view, but your puts prints to STDOUT which is not going to be your view, but the console screen your server is running in =)

If you want your view to show a list of the seven days, you need to actually create the string that does that and RETURN that :)

def next_seven_days
  outputstr = ""
  today = Date.today
  (today..(today+7.days)).each { |date| outputstr += date.to_s }
  return outputstr 
end 
answered 2011-07-07T18:46:53.750

Your Answer