# Style Question

**URL:** <https://rubytalk.org/t/style-question/13428>\
**Category:** ruby-talk\
**Created:** [5 September 2004 11:25 UTC](https://rubytalk.org/t/style-question/13428 "2004-09-05T11:25:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Schroeder](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Brian\_Schroeder](https://rubytalk.org/u/Brian_Schroeder)\
**Post date:** [5 September 2004 11:25 UTC](https://rubytalk.org/t/style-question/13428/1 "2004-09-05T11:25:13Z")

</div>

Hello everybody,

I've got a stylistic question. I know that "the ruby way" to implement  
iterations is using .each and similar things, but sometimes I like to use

for i in 0...n  
&nbsp;&nbsp;for j in i...n  
&nbsp;&nbsp;&nbsp;&nbsp;do something with i and for  
&nbsp;&nbsp;end  
end

Thats nice.

But if I want to iterate downwards the only way I found is 10.downto(0) do

> i | end

That seems not as intuitive as the for notation. Especially if I want ruby  
to teach algorithms to people who know how to read pseudo code, then I'd  
like something like

> for i in 10.downto 0  
> for i in 0.upto 10

I think this has been discussed before, but I'd like to now which style  
you use, or if I've missed the solution.

regards,

Brian

PS: How is for implemented? Can I maybe teach Number.downto to return an  
object that  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is used by for to iterate like an inverse range?

> **···**
>
> --  
> Brian Schröder  
> [http://www.brian-schroeder.de/](http://www.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [5 September 2004 13:10 UTC](https://rubytalk.org/t/style-question/13428/2 "2004-09-05T13:10:13Z")

</div>

Brian Schroeder wrote:

> Hello everybody,

Moin!

> I've got a stylistic question. I know that "the ruby way" to implement  
> iterations is using .each and similar things, but sometimes I like to use
> 
> for i in 0...n  
> &nbsp;&nbsp;for j in i...n  
> &nbsp;&nbsp;&nbsp;&nbsp;do something with i and for  
> &nbsp;&nbsp;end  
> end
> 
> Thats nice.
> 
> But if I want to iterate downwards the only way I found is 10.downto(0) do  
> \> i | end

What about this:

for i in (0...n).to\_a.reverse  
&nbsp;&nbsp;&nbsp;for j in (i...n).to\_a.reverse  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [i, j]  
&nbsp;&nbsp;&nbsp;end  
end

The problem with that is that it will be slow for huge ranges.

Or maybe this:

require 'enumerator'  
for i in (n - 1).enum\_for(:downto, 0)  
&nbsp;&nbsp;&nbsp;for j in (n - 1).enum\_for(:downto, i)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [i, j]  
&nbsp;&nbsp;&nbsp;end  
end

Or this one:

class Range  
&nbsp;&nbsp;&nbsp;def reverse\_each(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless last.respond\_to?(:downto)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise(TypeError, "cannot reverse iterate from #{last.class}")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is\_first = true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last.downto(first) do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if is\_first and exclude\_end?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is\_first = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block.call(item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

require 'enumerator'  
for i in (0...n).enum\_for(:reverse\_each)  
&nbsp;&nbsp;&nbsp;for j in (i...n).enum\_for(:reverse\_each)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [i, j]  
&nbsp;&nbsp;&nbsp;end  
end

And I think that Range should provide reverse\_each in Standard Ruby... (There's no problem in it when using #downto -- it is only defined for Integers, not for Strings.)

Regards,  
Florian Gross

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [5 September 2004 13:11 UTC](https://rubytalk.org/t/style-question/13428/3 "2004-09-05T13:11:53Z")

</div>

Hi --

> Hello everybody,
> 
> I've got a stylistic question. I know that "the ruby way" to implement  
> iterations is using .each and similar things, but sometimes I like to use
> 
> for i in 0...n  
> &nbsp;&nbsp;for j in i...n  
> &nbsp;&nbsp;&nbsp;&nbsp;do something with i and for  
> &nbsp;&nbsp;end  
> end
> 
> Thats nice.
> 
> But if I want to iterate downwards the only way I found is 10.downto(0) do  
> \> i | end
> 
> That seems not as intuitive as the for notation. Especially if I want ruby  
> to teach algorithms to people who know how to read pseudo code, then I'd  
> like something like  
> \> for i in 10.downto 0  
> \> for i in 0.upto 10
> 
> I think this has been discussed before, but I'd like to now which style  
> you use, or if I've missed the solution.

I would have thought that

&nbsp;&nbsp;10.downto(0).each do |i|

would be very pseudo-coder friendly. Have you determined for certain  
that the people you're teaching can't grasp this? You wouldn't even  
have to explain range notation 🙂

> PS: How is for implemented? Can I maybe teach Number.downto to return an  
> object that  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is used by for to iterate like an inverse range?

You could wrap it, like this:

&nbsp;&nbsp;class Integer  
&nbsp;&nbsp;&nbsp;&nbsp;alias olddownto downto  
&nbsp;&nbsp;&nbsp;&nbsp;def downto(x,&b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;olddownto(x,&b) if b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(x..self).to\_a.reverse  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

David

> **···**
>
> On Sun, 5 Sep 2004, Brian Schroeder wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [5 September 2004 19:40 UTC](https://rubytalk.org/t/style-question/13428/4 "2004-09-05T19:40:12Z")

</div>

"Brian Schroeder" \<spam0504@bssoftware.de\> schrieb im Newsbeitrag news:pan.2004.09.05.11.20.28.668820@bssoftware.de...

> Hello everybody,
> 
> I've got a stylistic question. I know that "the ruby way" to implement  
> iterations is using .each and similar things, but sometimes I like to use
> 
> for i in 0...n  
> for j in i...n  
> do something with i and for  
> end
> 
> Thats nice.
> 
> But if I want to iterate downwards the only way I found is 10.downto(0) do  
> \> i | end
> 
> That seems not as intuitive as the for notation. Especially if I want ruby  
> to teach algorithms to people who know how to read pseudo code, then I'd  
> like something like
> 
> > for i in 10.downto 0  
> > for i in 0.upto 10
> 
> I think this has been discussed before, but I'd like to now which style  
> you use, or if I've missed the solution.
> 
> regards,
> 
> Brian
> 
> PS: How is for implemented? Can I maybe teach Number.downto to return an  
> object that  
> &nbsp;&nbsp;&nbsp;is used by for to iterate like an inverse range?

Even more straightforward: you can implement a range that supports both directions yourself plus arbitrary stepping like this:

class Rg  
&nbsp;&nbsp;include Enumerable

&nbsp;&nbsp;def initialize(from, to, step = sign(to - from))  
&nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError, "Invalid step #{step}" if (to - from) \* step \<= 0  
&nbsp;&nbsp;&nbsp;&nbsp;@from, @to, @step = from, to, step  
&nbsp;&nbsp;&nbsp;&nbsp;@to += @step - ((@to - @from) % @step)  
&nbsp;&nbsp;end

&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;x = @from  
&nbsp;&nbsp;&nbsp;&nbsp;until x == @to  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += @step  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;end

&nbsp;&nbsp;def sign(x)  
&nbsp;&nbsp;&nbsp;&nbsp;case  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x \> 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-1  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

module Kernel  
private  
&nbsp;&nbsp;def Rg(from, to, \*step) Rg.new(from, to, \*step) end  
end

Then you can do this:

> > for i in Rg 0, 10, 2  
> > &nbsp;&nbsp;puts i  
> > end

0  
2  
4  
6  
8  
10  
=\> #\<Rg:0x10186da0 @from=0, @step=2, @to=12\>

> > for i in Rg( 0, -10, -3 )  
> > &nbsp;&nbsp;puts i  
> > end

0  
-3  
-6  
-9  
=\> #\<Rg:0x101801a0 @from=0, @step=-3, @to=-12\>

> > for i in Rg 0, -10  
> > puts i  
> > end

0  
-1  
-2  
-3  
-4  
-5  
-6  
-7  
-8  
-9  
-10  
=\> #\<Rg:0x10199ce8 @step=-1, @to=-11, @from=0\>

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [5 September 2004 19:50 UTC](https://rubytalk.org/t/style-question/13428/5 "2004-09-05T19:50:12Z")

</div>

Robert Klemme wrote:

> > Hello everybody,

Moin!

> > PS: How is for implemented? Can I maybe teach Number.downto to return an  
> > object that  
> > &nbsp;&nbsp;&nbsp;is used by for to iterate like an inverse range?
> 
> Even more straightforward: you can implement a range that supports both directions yourself plus arbitrary stepping like this:

I'd prefer using an enumerator around Range#step or Fixnum#step instead.

Here are a few small comments and suggestion about your code. Maybe they are useful.

> class Rg  
> include Enumerable
> 
> def initialize(from, to, step = sign(to - from))

What about String Ranges?

> &nbsp;&nbsp;&nbsp;raise ArgumentError, "Invalid step #{step}" if (to - from) \* step \<= 0  
> &nbsp;&nbsp;&nbsp;@from, @to, @step = from, to, step  
> &nbsp;&nbsp;&nbsp;@to += @step - ((@to - @from) % @step)  
> end
> 
> def each  
> &nbsp;&nbsp;&nbsp;x = @from  
> &nbsp;&nbsp;&nbsp;until x == @to

This is a problem -- there are Ranges that will never satisfy this condition. (Float ranges come to mind)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield x  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += @step  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;self  
> end
> 
> def sign(x)  
> &nbsp;&nbsp;&nbsp;case  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x \> 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-1  
> &nbsp;&nbsp;&nbsp;end  
> end

def sign(x); x \<=\> 0; end

> end

> Kind regards  
> &nbsp;&nbsp;&nbsp;robert

More regards,  
Florian Gross

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [5 September 2004 19:55 UTC](https://rubytalk.org/t/style-question/13428/6 "2004-09-05T19:55:15Z")

</div>

"Florian Gross" \<flgr@ccan.de\> schrieb im Newsbeitrag news:2q18quFqav8gU1@uni-berlin.de...

> Robert Klemme wrote:
> 
> > > Hello everybody,
> 
> Moin!
> 
> > > PS: How is for implemented? Can I maybe teach Number.downto to return an  
> > > object that  
> > > &nbsp;&nbsp;&nbsp;is used by for to iterate like an inverse range?
> > 
> > Even more straightforward: you can implement a range that supports both directions yourself plus arbitrary stepping like this:
> 
> I'd prefer using an enumerator around Range#step or Fixnum#step instead.
> 
> Here are a few small comments and suggestion about your code. Maybe they are useful.
> 
> > class Rg  
> > include Enumerable
> > 
> > def initialize(from, to, step = sign(to - from))
> 
> What about String Ranges?

That was just meant as an example - implementing integer ranges. I probably should have pointed that out more clearly.

> > &nbsp;&nbsp;&nbsp;raise ArgumentError, "Invalid step #{step}" if (to - from) \* step \<= 0  
> > &nbsp;&nbsp;&nbsp;@from, @to, @step = from, to, step  
> > &nbsp;&nbsp;&nbsp;@to += @step - ((@to - @from) % @step)  
> > end
> > 
> > def each  
> > &nbsp;&nbsp;&nbsp;x = @from  
> > &nbsp;&nbsp;&nbsp;until x == @to
> 
> This is a problem -- there are Ranges that will never satisfy this condition. (Float ranges come to mind)

Yep, the int check is missing.

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield x  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x += @step  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;self  
> > end
> > 
> > def sign(x)  
> > &nbsp;&nbsp;&nbsp;case  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x \> 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when x == 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-1  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> 
> def sign(x); x \<=\> 0; end

Very nice! Then we don't need sign():

def initialize(from, to, step = ((to - from) \<=\> 0))

> > end
> 
> > Kind regards  
> > &nbsp;&nbsp;&nbsp;robert
> 
> More regards,  
> Florian Gross

EMR (even more regards)

&nbsp;&nbsp;&nbsp;&nbsp;robert
