From: "David A. Black" Date: 2009-06-24T02:40:47+09:00 Subject: Re: Is it the ruby way to implement #downto Hi -- On Wed, 24 Jun 2009, Li Chen wrote: > 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? def my_downto(n) (-self..-n).each {|i| yield -i } self end :-) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com