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

7
  • Using your exact code, I get the desired results.
    – Aaron
    Jul 7, 2011 at 18:34
  • Don't know what your problem is. I get this result:2011-07-07 2011-07-08 2011-07-09 2011-07-10 2011-07-11 2011-07-12 2011-07-13 2011-07-14
    – phlogratos
    Jul 7, 2011 at 18:34
  • What are you using to run your code?
    – Aaron
    Jul 7, 2011 at 18:37
  • 1
    Yeah, I thought it was weird. I'm calling the helper method in the view this way <%= next_seven_days %>
    – Slick23
    Jul 7, 2011 at 18:38
  • It's working in the IRB, but not the view.
    – Slick23
    Jul 7, 2011 at 18:39

5 Answers 5

10

I think you want something more like this:

def next_seven_days
  today = Date.today
  (today .. today + 7).inject { |init, date|  "#{init} #{date}" } 
end

In this case, the return value is a concatenated string containing all the dates.

Alternatively, if it's not a concatenated string you want, you could change the "#{init} #{date}" part of it.

As a side note, using puts in ruby on rails won't print to the web page. When you use <%= next_seven_days %>, the return value of that function is what will be printed to the page. The each function returns the range in parentheses.

2
  • 2
    I think .to_a.join(" ") would be more readable than using inject for this.
    – sepp2k
    Jul 7, 2011 at 18:51
  • You're probably right. I have a feeling the OP doesn't really want a string list of dates, and is currently trying to make sure they're correctly iterating through the dates before moving on to the next step. If they really want a list of dates, collect would be the way to go.
    – Aaron
    Jul 7, 2011 at 18:54
3

Your code will definitely print all eight days to stdout. Your problem is that you're looking at the return value (which since each returns self will be (today .. today + 7)).

Note that if you print to stdout inside an erb template, that won't cause the output to show up in the rendered template.

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 
1
def next_seven_days
  today = Date.today
  (today..(today+7.days)).each { |date| puts date } 
end 
2
  • I'm just getting 2011-07-07..2011-07-14 with that in my view.
    – Slick23
    Jul 7, 2011 at 18:32
  • 1
    Note that the .days call is unneeded - it's valid to add an integer to a date, it just increases the days by that amount.
    – ryeguy
    Jul 7, 2011 at 18:34
0

Use the "next" method

        def next_seven_days
          today= Date.today
          7.times do 
             puts today
             today=today.next
           end
        end    

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.