[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

split up days

misiek

1/24/2006 4:39:00 PM

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
12 Answers

Robert Klemme

1/24/2006 4:49:00 PM

0

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

misiek

1/24/2006 5:21:00 PM

0

Robert Klemme wrote:
> 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
>


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

??

1/24/2006 6:26:00 PM

0

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?

Robert Klemme

1/24/2006 6:58:00 PM

0

2006/1/24, misiek <michaelaugustyniak@gazeta.pl>:

> 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

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

--
Have a look: http://www.flickr.com/photos/fu...


Sky Yin

1/24/2006 7:06:00 PM

0

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

:^)

On 1/24/06, misiek <michaelaugustyniak@gazeta.pl> wrote:
> Robert Klemme wrote:
> > 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
> >
>
>
> 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
>
>


--
Blog >>> http://spaces.msn.com/members/s...


misiek

1/24/2006 9:29:00 PM

0

Sky Yin wrote:
> 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
>
> :^)
>

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

Sky Yin

1/24/2006 10:19:00 PM

0

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?

On 1/24/06, misiek <michaelaugustyniak@gazeta.pl> wrote:
> Sky Yin wrote:
> > 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
> >
> > :^)
> >
>
> 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
>
>


--
Blog >>> http://spaces.msn.com/members/s...


Sky Yin

1/24/2006 10:21:00 PM

0

oops, the last line should be:

days = (Date.new(date_start)..Date.new(date_stop)).to_a


On 1/24/06, Sky Yin <sky.yin@gmail.com> wrote:
> 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?
>
> On 1/24/06, misiek <michaelaugustyniak@gazeta.pl> wrote:
> > Sky Yin wrote:
> > > 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
> > >
> > > :^)
> > >
> >
> > 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
> >
> >
>
>
> --
> Blog >>> http://spaces.msn.com/members/s...
>


--
Blog >>> http://spaces.msn.com/members/s...


misiek

1/24/2006 11:11:00 PM

0

Sky Yin wrote:
> oops, the last line should be:
>
> days = (Date.new(date_start)..Date.new(date_stop)).to_a
>
>
> On 1/24/06, Sky Yin <sky.yin@gmail.com> wrote:
>
>>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?
>>

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}
^

misiek

1/24/2006 11:13:00 PM

0

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 :(