# Substring by range parameter (bug?)

**URL:** <https://rubytalk.org/t/substring-by-range-parameter-bug/12694>\
**Category:** ruby-talk\
**Created:** [21 July 2004 19:20 UTC](https://rubytalk.org/t/substring-by-range-parameter-bug/12694 "2004-07-21T19:20:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_T](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@D\_T](https://rubytalk.org/u/D_T)\
**Post date:** [21 July 2004 19:20 UTC](https://rubytalk.org/t/substring-by-range-parameter-bug/12694/1 "2004-07-21T19:20:06Z")

</div>

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/](http://search.msn.click-url.com/go/onm00200636ave/direct/01/)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard3](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@Ara.T.Howard3](https://rubytalk.org/u/Ara.T.Howard3)\
**Post date:** [21 July 2004 20:22 UTC](https://rubytalk.org/t/substring-by-range-parameter-bug/12694/2 "2004-07-21T20:22:03Z")

</div>

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

eg. for the string "foobar":

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

&nbsp;&nbsp;&nbsp;~ \> irb  
&nbsp;&nbsp;&nbsp;irb(main):001:0\> "foobar"[-2..-1]  
&nbsp;&nbsp;&nbsp;=\> "ar"  
&nbsp;&nbsp;&nbsp;irb(main):002:0\> "foobar"[-6..-4]  
&nbsp;&nbsp;&nbsp;=\> "foo"  
&nbsp;&nbsp;&nbsp;irb(main):003:0\> "foobar"[-3..-1]  
&nbsp;&nbsp;&nbsp;=\> "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
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Sam\_Sungshik\_Kong](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sam\_Sungshik\_Kong](https://rubytalk.org/u/Sam_Sungshik_Kong)\
**Post date:** [21 July 2004 20:52 UTC](https://rubytalk.org/t/substring-by-range-parameter-bug/12694/3 "2004-07-21T20:52:02Z")

</div>

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":
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---- --- ----  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char idx nidx  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;---- --- ----  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f 0 -6  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o 1 -5  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o 2 -4  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b 3 -3  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a 4 -2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r 5 -1
> > 
> > &nbsp;&nbsp;&nbsp;~ \> irb  
> > &nbsp;&nbsp;&nbsp;irb(main):001:0\> "foobar"[-2..-1]  
> > &nbsp;&nbsp;&nbsp;=\> "ar"  
> > &nbsp;&nbsp;&nbsp;irb(main):002:0\> "foobar"[-6..-4]  
> > &nbsp;&nbsp;&nbsp;=\> "foo"  
> > &nbsp;&nbsp;&nbsp;irb(main):003:0\> "foobar"[-3..-1]  
> > &nbsp;&nbsp;&nbsp;=\> "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
> 
> # ============================================================================
