Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby-Proper Usage of the Date.step() method

I have been trying to use the step() method on Date object to retrieve the previous 2 dates from the current date as follows:

  date_d.step(2, step=-2){|d|
    puts d        
  }

where 2 is the limit and step is the number of steps backward or forward.

I have done this in accordance with the Documentation given here: Date.step()

This code snippet goes into an infinite loop and then outputs the date non-stop (backwards)

There doesn't seem to be enough documentation for this method and i am not finding solutions online as well.

Please help me out with this.

like image 642
dkris Avatar asked Aug 31 '10 06:08

dkris


People also ask

How do I get the current date in Ruby?

Ruby | DateTime now() function DateTime#now() : now() is a DateTime class method which returns a DateTime object denoting the given calendar date. Return: DateTime object denoting the given calendar date.


1 Answers

the limit parameter is the date where the loop stops not the number of days or iterations. so for example

date_d = Date.parse( '2010-08-01' )
date_d.step(date_d - 4 , step=-2){|d|
   puts d        
}

will output

2010-08-01
2010-07-30
2010-07-28
like image 147
Nikolaus Gradwohl Avatar answered Oct 02 '22 00:10

Nikolaus Gradwohl