Forum: Ruby how to iterate on date ranges

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-01-28 06:18
Hi...here me again
If user enters, 2 date parameters.i want to find all the folders between
these date ranges.
For example if the user enters 2009-01-02 and 2009-01-30.i want to make
check between these two dates.
I have done below code for single date parameter.but i don't know how to
iterate for two date ranges.
Dir.foreach("ChatHistory") do |folder_name|
  if folder_name == params[:e_date][1]
  @sub_folder = Array.new
  Dir.foreach(File.join("ChatHistory",folder_name)) do |sub_folder_name|
  @sub_folder << sub_folder_name
  end
  end
end
pls kindly assist me friends.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-01-28 06:59
(Received via mailing list)
2009/1/28 Newb Newb <revathy.p@angleritech.com>:
>  Dir.foreach(File.join("ChatHistory",folder_name)) do |sub_folder_name|
>  @sub_folder << sub_folder_name
>  end
>  end
> end
You can make array of dates between two dates like this:

require 'date'
arr = ('2009-01-02'..'2009-01-30').select{|x| Date.parse(x) rescue nil}


Regards,

Park Heesob
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-28 07:19
(Received via mailing list)
On Jan 28, 2009, at 12:56 AM, Heesob Park wrote:

>> iterate for two date ranges.
>
> require 'date'
> arr = ('2009-01-02'..'2009-01-30').select{|x| Date.parse(x) rescue
> nil}
>
>
> Regards,
>
> Park Heesob

Why iterate a range of Strings when you can just use Dates!

irb> require 'date'
=> true
irb> (Date.parse('2009-01-02') .. Date.parse('2009-01-31')).map{|d|
d.to_s}
=> ["2009-01-02", "2009-01-03", "2009-01-04", "2009-01-05",
"2009-01-06", "2009-01-07", "2009-01-08", "2009-01-09", "2009-01-10",
"2009-01-11", "2009-01-12", "2009-01-13", "2009-01-14", "2009-01-15",
"2009-01-16", "2009-01-17", "2009-01-18", "2009-01-19", "2009-01-20",
"2009-01-21", "2009-01-22", "2009-01-23", "2009-01-24", "2009-01-25",
"2009-01-26", "2009-01-27", "2009-01-28", "2009-01-29", "2009-01-30",
"2009-01-31"]
irb> puts (Date.parse('2009-01-02') .. Date.parse('2009-03-31'))
2009-01-02..2009-03-31
=> nil
irb> puts (Date.parse('2009-01-02') .. Date.parse('2009-03-31')).to_a

I don't understand what you're trying to do

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.