Forum: Ruby split up days

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.
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-24 18:35
(Received via mailing list)
hi

problem is ....

I got date @start and date @stop as a date and I can display like

@start = Thu Jan 19 00:00:00 CST 2006
@stop = Tue Jan 21 00:00:00 CST 2006


how to split up this days and put to array ?

array[] = 	"Thu Jan 19 00:00:00 CST 2006
		 Wed Jan 20 00:00:00 CST 2006
		 Tue Jan 21 00:00:00 CST 2006"

is there some function which can do that ?

thanks for help
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-24 18:38
(Received via mailing list)
misiek wrote:
> hi
>
> problem is ....
>
> I got date @start and date @stop as a date and I can display like
>
> @start = Thu Jan 19 00:00:00 CST 2006
> @stop = Tue Jan 21 00:00:00 CST 2006

This is not valid Ruby code.  What types are these objects?

> how to split up this days and put to array ?
>
> array[] = "Thu Jan 19 00:00:00 CST 2006
> Wed Jan 20 00:00:00 CST 2006
> Tue Jan 21 00:00:00 CST 2006"

You assign a string but you seem to want an array.  Also, you don't seem
to want to split but to append.

# split at whitespace
array = @start.to_s.split /\s+/

# split into array, works for Time
array = @start.to_a

# put into array
array = [@start, @stop]

# concatenate
array = "#{@start}\n#{@stop}"

> is there some function which can do that ?

If you tells us what you actually want, we might be able to come up with
an answer.

Cheers

    robert
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-24 18:48
(Received via mailing list)
Robert Klemme wrote:
>
>
> array = [@start, @stop]
>
> Cheers
>
>     robert
>


thank you for answer .
from FORM I can select days, FROM - TO
like from Monday to Friday
exanmple:

  from    2006-01-20    to    2006-01-24

and than I need to put each day to database as five days like

  2006-01-20
  2006-01-21
  2006-01-22
  2006-01-23
  2006-01-24

so first I need to put each day to array and that in loop put each day
to database.

this is what I want
thanks for help
Ce58060fd74b53cb3fec2e2f6c9ca93d?d=identicon&s=25 ç??ä¸? (Guest)
on 2006-01-24 19:30
(Received via mailing list)
def toSecond(stime)
 atime=stime.split("-")
 return Time.local(atime[0],atime[1],atime[2]).to_i
end

@start = "2006-01-20"
@stop = "2006-01-24"

array=Array.new
toSecond(@start).step(toSecond(@stop),86400) do |t|
 array.push(Time.at(t).strftime("%Y-%m-%d"))
end

puts array

# Is this ok?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-24 20:00
(Received via mailing list)
2006/1/24, misiek <michaelaugustyniak@gazeta.pl>:

>   2006-01-22
>   2006-01-23
>   2006-01-24
>
> so first I need to put each day to array and that in loop put each day
> to database.
>
> this is what I want
> thanks for help

Now we're cooking.  How's that?

require 'date'
# fetch start and end date from somewhere
start=Date.today
endd=start+30
d=start
while d < endd
  puts d
  d += 5
end

robert
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-24 20:13
(Received via mailing list)
Range can help you. Example:

>>require 'date'
>>days = (Date.new(2006,1,20)..Date.new(2006,1,24)).to_a
>>days.each {|date| puts date}
=>2006-01-20
=>2006-01-21
=>2006-01-22
=>2006-01-23
=>2006-01-24

:^)
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-24 22:35
(Received via mailing list)
Sky Yin wrote:
> =>2006-01-23
> =>2006-01-24
>
> :^)
>

this is super but why when I will put 2006,3,2 into variable
like @start = "2006,1,3" and @stop = "2006,1,4" does not work ?
days = (Date.new(@start)..Date.new(@stop)).to_a


undefined method `-' for "2006,1,3":String
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-24 23:21
(Received via mailing list)
It's easy to transfer the string "year, month, day" to the parameter
format that meets the requirement of Date.new(year, month, day) :

date_start = @start.split(',').map {|x|, x.to_i}
date_stop = @stop.split(',').map {|x|, x.to_i}
days = (Date.new(@start)..Date.new(@stop)).to_a

Still elegant?
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-24 23:24
(Received via mailing list)
oops, the last line should be:

days = (Date.new(date_start)..Date.new(date_stop)).to_a
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-25 00:15
(Received via mailing list)
Sky Yin wrote:
>>date_start = @start.split(',').map {|x|, x.to_i}
>>date_stop = @stop.split(',').map {|x|, x.to_i}
>>days = (Date.new(@start)..Date.new(@stop)).to_a
>>
>>Still elegant?
>>

I got like

@start = @params['start']['year'].to_s + "," +
@params['start']['month'].to_s + "," + @params['start']['day'].to_s

@stop = @params['stop']['year'].to_s + "," +
@params['stop']['month'].to_s + "," + @params['stop']['day'].to_s


and I try to put to
days = (Date.new(@start)..Date.new(@stop)).to_a

still got this error  undefined method `-' for "2006,1,3":String

BTW what is "x" => date_start = @start.split(',').map {|x|, x.to_i}  ?
                                                         ^   ^

or is going about (',') ? hmm

because is error
../script/../config/../app/controllers/hour_controller.rb:178: parse
error, unexpected ','
		date_start = @start.split(',').map {|x|, x.to_i}
		                                        ^
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-25 00:18
(Received via mailing list)
ups to put to

date_start = @start.split(',').map {|x|, x.to_i}

I try format like this

@start =
Time.local(@params['start']['year'],@params['start']['month'],@params['start']['day'])

the same error :(
9aa1e92a0ebb17fdaad2fa39b4c52b8a?d=identicon&s=25 misiek (Guest)
on 2006-01-25 00:21
(Received via mailing list)
I removed , from date_start = @start.split(',').map {|x|  x.to_i}
                                                          ^

now got
private method `split' called for Tue Jan 24 00:00:00 CST 2006:Time
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-25 00:36
(Received via mailing list)
sry, I copied a wrong part of my own code. What I tried to do is to
split the string "year, month, day" into 3 integers and store in an
array [year, month, day], then create Date objects from the arrays.

def new_date(date)
  Date.new(date[0], date[1], date[2])
end

date_start = @start.split(',').map {|x|, x.to_i}
date_stop = @stop.split(',').map {|x|, x.to_i}
days = (new_date(date_start)..new_date(date_stop)).to_a

This should work now.
This topic is locked and can not be replied to.