# Question on Ranges

**URL:** <https://www.ruby-forum.com/t/question-on-ranges/141990>\
**Category:** Rails\
**Created:** [July 2, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990 "2008-07-02T12:59:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [July 2, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990/1 "2008-07-02T12:59:22Z")

</div>

Why am I not able to do this?

x=Time.now  
y=Time.now + 1.year  
(x…y).step(1.week)

The step function just goes seems to hang in a loop. Shouldn’t it  
return the remaining weeks in the current year (ignoring the start of  
week)?

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [July 2, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990/2 "2008-07-02T13:09:55Z")

</div>

It’s not hanging, it’s stepping through the range. You need to pass  
it a block to tell it what to do on each step.

(x…y).step(1.week) { |x| puts x }

Best.  
Mike

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [July 2, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990/3 "2008-07-02T13:25:45Z")

</div>

In my actual code, I was trying to store the results in an array and  
it just takes forever. Does it work properly for you? Even if I  
iterate through the array members using a block, each block takes a  
_long_ time to display something.

Ruby --\> ruby 1.8.6 (2008-03-03 patchlevel 114) [x86\_64-linux]  
Rails --\> Rails 2.1.0  
OS --\> Fedora 7 x64

I tried this on a fresh rails app as well.

Thanks,  
Mukund

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [July 2, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990/4 "2008-07-02T13:46:18Z")

</div>

On 2 Jul 2008, at 12:25, Mukund wrote:

> In my actual code, I was trying to store the results in an array and  
> it just takes forever. Does it work properly for you? Even if I  
> iterate through the array members using a block, each block takes a  
> _long_ time to display something.
> 
> Ruby --\> ruby 1.8.6 (2008-03-03 patchlevel 114) [x86\_64-linux]  
> Rails --\> Rails 2.1.0  
> OS --\> Fedora 7 x64

Not working at all or just very slow? With a non numeric range, ruby  
calls the succ method as many times as needed per iteration (ie 600000  
or so in this case) which could conceivably be slow. You might be  
better off stepping through a numeric offset that you can add to the  
origin point, ie

(0…1).step(1.week) do |increment|  
#do something to x.increment  
end

I have an inkling that neither this (not the slow method) will handle  
dst changes properly as they both force 1.week to an integer.  
Fred

---

<div class="post-metadata">

**Author:** ![Mukund](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Mukund](https://www.ruby-forum.com/u/Mukund)\
**Post date:** [July 3, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/question-on-ranges/141990/5 "2008-07-03T14:19:39Z")

</div>

Just incredibly slow. I ended up doing it the hard/right way but  
converting time to integer seconds since epoch at the expense of  
readability.

On Jul 2, 4:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
