Forum: Ruby reverse indexing (was RE: Ruby and WMI)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
botp (Guest)
on 2005-11-23 04:34
(Received via mailing list)
#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
Sean O. (Guest)
on 2005-11-23 10:36
(Received via mailing list)
On 11/23/05, Peña, Botp <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
This topic is locked and can not be replied to.