# Reverse indexing (was RE: Ruby and WMI)

**URL:** <https://www.ruby-forum.com/t/reverse-indexing-was-re-ruby-and-wmi/52986>\
**Category:** Ruby\
**Created:** [November 23, 2005, 3:34am UTC](https://www.ruby-forum.com/t/reverse-indexing-was-re-ruby-and-wmi/52986 "2005-11-23T03:34:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 23, 2005, 3:34am UTC](https://www.ruby-forum.com/t/reverse-indexing-was-re-ruby-and-wmi/52986/1 "2005-11-23T03:34:35Z")

</div>

#C:\family\ruby\>irb --simple-prompt  
#\>\> x=[0,1,2,3,4,5]  
#=\> [0, 1, 2, 3, 4, 5]  
…

#otherwise, these will return empty arrays

# 

#\>\> x[-1…2]  
#=\> []

# 

#\>\> x[-1…-5]  
#=\> []

# 

#\>\> x[-1…-4]  
#=\> []

# 

on the second thought, what if ruby allows the above such that,

considering index equivalence for x:

0 1 2 3 4 5  
-6 -5 -4 -3 -2 -1

> > x[-1…2]  
> > =\> [5,4,3,2]

> > x[-1…-5]  
> > =\> [5,4,3,2,1]

> > x[-1…-6].each{|x| print x}  
> > 543210 =\>[5,4,3,2,1,0]

and since arrays are ordered, we may have notation for reverse traversal  
of array …

dunno, just nuby here.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/258eb7/32.png) [@botp](https://www.ruby-forum.com/u/botp)\
**Post date:** [November 23, 2005, 9:36am UTC](https://www.ruby-forum.com/t/reverse-indexing-was-re-ruby-and-wmi/52986/2 "2005-11-23T09:36:41Z")

</div>

On 11/23/05, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and since arrays are ordered, we may have notation for reverse traversal of array …

Did you know you can use reverse\_each?

x = (0…5).to\_a  
x.reverse\_each do |i|  
print i  
end  
#=\> 54321

Regards,

Sean
