Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/hash/add_to_ruby.rb

Instance Method Summary collapse

Instance Method Details

#random(days) ⇒ Object

It will generate a random date In case days is a Date it will generate until that date In case days is an Integer it will generate from the self date + the number of days specified examples: puts Date.today.random(60) # random date from today to 60 days after puts Date.strptime('01-09-2005', '%d-%m-%Y').random(100) puts Date.new(2003,10,31).random(Date.today) #Random date from 2003/10/31 to today



106
107
108
109
110
111
112
113
114
115
# File 'lib/nice/hash/add_to_ruby.rb', line 106

def random(days)
  if days.kind_of?(Date) then
    dif_dates = self - (days+1)
    date_result = self + rand(dif_dates)
    return date_result
  elsif days.kind_of?(Integer) then
    date_result = self + rand(days+1)
    return date_result
  end
end