[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: substring: to the end of the string

Austin Ziegler

11/17/2003 4:33:00 AM

On Mon, 17 Nov 2003 11:40:13 +0900, Jon A. Lambert wrote:
> Actually I find this behavior annoying:
> irb(main):015:0> "hello"[3..-1]
> => "lo"
> irb(main):016:0> "hello"[4..-1]
> => "o"
> irb(main):017:0> "hello"[5..-1]
> => ""
> irb(main):018:0> "hello"[6..-1]
> => nil

This has to do with how Ruby views indices on arrays and array-like
structures (like strings).

[h] [e] [l] [l] [o]
0 1 2 3 4 5

The indices are "before" the actual element, so (5 .. -1) represents
"end of the string to end of the string", whereas (6 .. -1)
reperesents (outside fo the string to the end of the string).

Some of this has to do with performance, I think, but it's something
that makes sense, once you're used to it. I think that it's more
sensible than the typical implementations.

-austin
--
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.16
* 23.27.28