Forum: Ruby 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.
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 botp (Guest)
on 2005-11-22 07:54
(Received via mailing list)
#Thank you, it now works, can you tell me one thing, ( I am learning)
#what does [8..-1] mean in the script "puts "\t" + mem.adsPath[8..-1]".

hope this is simple enough.

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


here, we access normally the array x.
the first index starts at 0.

>> x[0..1]
=> [0, 1]

we access the third (3-1) and fifth element (5-1)

>> x[2..4]
=> [2, 3, 4]

to access the element relative to the last position, prefix the index
with a minus sign. But there is -0, so start w -1.

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

>> x[2..-2]
=> [2, 3, 4]

it works too even if both are negative as long as start index is less
than or equal to ending index

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

>> x[-2..-1]
=> [4, 5]

>> x[-1..-1]
=> [5]

>> x[-4..-4]
=> [2]

otherwise, these will return empty arrays

>> x[-1..2]
=> []

>> x[-1..-5]
=> []

>> x[-1..-4]
=> []

hth.

kind regards -botp
This topic is locked and can not be replied to.