# Is it the ruby way to implement #downto

**URL:** <https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856>\
**Category:** Ruby\
**Created:** [June 23, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856 "2009-06-23T17:35:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/1 "2009-06-23T17:35:49Z")

</div>

Hi all,

I wonder if the following code is the ruby way to implement  
Integer#downto.

Thanks,

Li

#######################  
class Integer  
def my\_downto(final=2)  
for i in (final…self).to\_a.reverse  
yield i  
end  
end  
end

10.my\_downto(-1) do |i| puts i end

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 7:11pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/2 "2009-06-23T19:11:30Z")

</div>

On Jun 23, 8:36 am, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> 10.my\_downto(-1) do |i| puts i end

Well, I’d hope that creating 3 unnecessary objects wouldn’t be deemed  
the Ruby way :^)  
Most code I’ve seen would use each() instead of for i in (). Each is  
faster anyways.

Why not use the bastardized C way:

class Integer  
def my\_downto(final=2)  
i = self  
while i \>= final  
yield i  
i-=1  
end  
end  
end

I don’t believe the Ruby community has shunned the idea of writing C  
in ruby have they?

-Skye

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/3 "2009-06-23T19:32:43Z")

</div>

Skye Shaw!@#$ wrote:

> Well, I’d hope that creating 3 unnecessary objects wouldn’t be deemed  
> the Ruby way :^)  
> Most code I’ve seen would use each() instead of for i in (). Each is  
> faster anyways.

I do it this way using each().

def my\_downto2(final=2)  
(final…self).to\_a.reverse.each do |i| yield i end  
end

But I am still uncomfortable that I have to use #to\_a followed by  
#reverse to do the trick. Besides using while loops what is the best way  
to add a new method of #downto?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/4 "2009-06-23T19:36:10Z")

</div>

Hi –

On Wed, 24 Jun 2009, Skye Shaw!@#$ wrote:

> > Â Â Â end  
> > Why not use the bastardized C way:
> 
> I don’t believe the Ruby community has shunned the idea of writing C  
> in ruby have they?

I think most of us shun doing the heavy lifting when Ruby can do it  
for us 🙂 There’s nothing C-specific about using an explicit loop  
variable, but it’s pretty uncommon in Ruby code, except when doing  
exercises like implementing each without recourse to each and things  
like that.

I’d be tempted to do something like:

def my\_downto(n)  
(self-n+1).times {|x| yield self-x }  
self  
end

I don’t know how it performs compared to the others.

David

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/5 "2009-06-23T19:42:03Z")

</div>

Hi –

On Wed, 24 Jun 2009, Li Chen wrote:

> end
> 
> But I am still uncomfortable that I have to use #to\_a followed by  
> #reverse to do the trick. Besides using while loops what is the best way  
> to add a new method of #downto?

def my\_downto(n)  
(-self…-n).each {|i| yield -i }  
self  
end

🙂

David

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 23, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/is-it-the-ruby-way-to-implement-downto/170856/6 "2009-06-23T22:33:33Z")

</div>

David A. Black wrote:

> def my\_downto(n)  
> (-self…-n).each {|i| yield -i }  
> self  
> end
> 
> 🙂
> 
> David

Hi David,

Thank you so much,

Li
