Substring by range parameter (bug?)

s="0123456789"
s[-1..-2] #=> ""
s[-2..-1] #=> "89"
s[-1..-3] #=> nil
s[-3..-1] #=> "789"
s[-1..-2] = "..." ; s #=> "012345678...9"

Expect s[-1..-2] returns nil as s[-1..-3]. (bug ? it returns empty string instead of nil.)

···

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/

for ranges, start must be <= end and -1 is not <= -2

eg. for the string "foobar":

     ---- --- ----
     char idx nidx
     ---- --- ----
     f 0 -6
     o 1 -5
     o 2 -4
     b 3 -3
     a 4 -2
     r 5 -1

   ~ > irb
   irb(main):001:0> "foobar"[-2..-1]
   => "ar"
   irb(main):002:0> "foobar"[-6..-4]
   => "foo"
   irb(main):003:0> "foobar"[-3..-1]
   => "bar"

cheers.

-a

···

On Thu, 22 Jul 2004, D T wrote:

s="0123456789"
s[-1..-2] #=> ""
s[-2..-1] #=> "89"
s[-1..-3] #=> nil
s[-3..-1] #=> "789"
s[-1..-2] = "..." ; s #=> "012345678...9"

Expect s[-1..-2] returns nil as s[-1..-3]. (bug ? it returns empty string
instead of nil.)

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Hi, Ara!

I understand what you mean.
However, the return values should be consistent even if the range object
doesn't make sense.

> s[-1..-2] #=> ""
> s[-1..-3] #=> nil

How would you explain the inconsistency?

The only explanation I can think of is:

s[-1..-1] #=>"9"
s[-1..-2] #=>"", because one step back, one less character...
s[-1..-3] #=>nil, because there's no "less character" concept beyond empty
string.

Sam

"Ara.T.Howard" <ahoward@noaa.gov> wrote in message
news:Pine.LNX.4.60.0407211408090.19495@harp.ngdc.noaa.gov...

> s="0123456789"
> s[-1..-2] #=> ""
> s[-2..-1] #=> "89"
> s[-1..-3] #=> nil
> s[-3..-1] #=> "789"
> s[-1..-2] = "..." ; s #=> "012345678...9"
>
> Expect s[-1..-2] returns nil as s[-1..-3]. (bug ? it returns empty

string

···

On Thu, 22 Jul 2004, D T wrote:
> instead of nil.)

for ranges, start must be <= end and -1 is not <= -2

eg. for the string "foobar":

     ---- --- ----
     char idx nidx
     ---- --- ----
     f 0 -6
     o 1 -5
     o 2 -4
     b 3 -3
     a 4 -2
     r 5 -1

   ~ > irb
   irb(main):001:0> "foobar"[-2..-1]
   => "ar"
   irb(main):002:0> "foobar"[-6..-4]
   => "foo"
   irb(main):003:0> "foobar"[-3..-1]
   => "bar"

cheers.

-a
--

============================================================================

> EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
> PHONE :: 303.497.6469
> A flower falls, even though we love it;
> and a weed grows, even though we do not love it.
> --Dogen

============================================================================