Hi --

On Sun, 5 Sep 2004, Brian Schroeder wrote:

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

  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
>  	  is used by for to iterate like an inverse range?

You could wrap it, like this:

  class Integer
    alias olddownto downto
    def downto(x,&b)
      olddownto(x,&b) if b
      return(x..self).to_a.reverse
    end
  end


David

-- 
David A. Black
dblack / wobblini.net