# Substrings by Position

**URL:** <https://www.ruby-forum.com/t/substrings-by-position/63051>\
**Category:** Ruby\
**Created:** [June 11, 2006, 6:33am UTC](https://www.ruby-forum.com/t/substrings-by-position/63051 "2006-06-11T06:33:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 6:33am UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/1 "2006-06-11T06:33:29Z")

</div>

Hi,

Just a logic question about substrings by position.

foo = “012345678”  
foo[5,1] #-\> “5”  
foo[-4,2] #-\> “56”  
foo[-3,2] = “” #-\> “0123458”

Shouldn’t a negative indexing work as well? (Below are just examples,  
they currently don’t work) It will allow strings to be REVERSED easily,  
and thus doing something new.

foo = “012345678”  
foo[-4,-2] #-\> “43”  
foo[-3,-2] = “” #-\> “0123678”  
foo[-4,-3] = “9” #-\> “93678”  
foo[1,-2] = “123” #-\> “3213678”  
foo = “01295678”  
foo[-4,-1] = “43” #-\> “012345678”

In other words,

foo = “012345678”  
foo[4,1] #-\> 4  
foo[4,-1] #-\> 3

Is this making sense?

Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/2 "2006-06-11T12:22:05Z")

</div>

2006/6/11, Charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi,
> 
> Just a logic question about substrings by position.
> 
> foo = “012345678”  
> foo[5,1] #-\> “5”  
> foo[-4,2] #-\> “56”  
> foo[-3,2] = “” #-\> “0123458”

What version are you on?

> > foo = “012345678”  
> > =\> “012345678”  
> > foo[-3,2]  
> > =\> “67”  
> > RUBY\_VERSION  
> > =\> “1.8.3”

Note also that you can use a range as well:

> > foo[-3…-1]  
> > =\> “678”  
> > foo[-3…-1]  
> > =\> “67”

> Shouldn’t a negative indexing work as well? (Below are just examples,  
> they currently don’t work) It will allow strings to be REVERSED easily,  
> and thus doing something new.

Negative indexes do work already. You probably mean negative lengths.

> foo = “012345678”  
> foo[4,1] #-\> 4  
> foo[4,-1] #-\> 3
> 
> Is this making sense?

Dunno. I personally never missed this behavior. I’m not sure whether  
there is much use for this and if it’s worth the effort. Note that the  
implementation of String#[] becomes even more complex than it is right  
now plus with the current design returned strings usually share the  
buffer with the original. You cannot do that with implicit reversed  
strings. I opt for a mild “no” but I’m not religious.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/3 "2006-06-11T14:36:45Z")

</div>

On Jun 11, 2006, at 13:08, Charlie wrote:

> > > Shouldn’t a negative indexing work as well? (Below are just  
> > > examples,  
> > > they currently don’t work) It will allow strings to be REVERSED  
> > > easily,  
> > > and thus doing something new.
> > 
> > Negative indexes do work already. You probably mean negative  
> > lengths.
> 
> Yes, you’re right. It just mean lengths with direction.

I don’t think that having negative lengths reflect direction is at  
all a natural decision; it’s describing the bounds of a substring,  
not describing and implicit iteration through individual characters.  
In other words, if negative lengths worked, I’d fully expect them to  
work like this (y is negative): foo[x, y] == foo[(x+y), y.abs]

e.g.

foo = “0123456789”  
foo[4,-1] # =\> 3  
foo[3, 1] # =\> 3  
foo[4,-2] # =\> 23  
foo[2, 2] # =\> 23

Conflating string reversal into setting the bounds of the substring  
is a mistake, I’d say, particularly since there’s already a perfectly  
reasonable String#reverse.

matthew smillie.

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/4 "2006-06-11T15:04:49Z")

</div>

Matthew S. wrote:

> On Jun 11, 2006, at 13:08, Charlie wrote:
> 
> > > > Shouldn’t a negative indexing work as well? (Below are just  
> > > > examples,  
> > > > they currently don’t work) It will allow strings to be REVERSED  
> > > > easily,  
> > > > and thus doing something new.
> > > 
> > > Negative indexes do work already. You probably mean negative  
> > > lengths.
> > 
> > Yes, you’re right. It just mean lengths with direction.
> 
> I don’t think that having negative lengths reflect direction is at  
> all a natural decision; it’s describing the bounds of a substring,  
> not describing and implicit iteration through individual characters.  
> In other words, if negative lengths worked, I’d fully expect them to  
> work like this (y is negative): foo[x, y] == foo[(x+y), y.abs]
> 
> e.g.
> 
> foo = “0123456789”  
> foo[4,-1] # =\> 3  
> foo[3, 1] # =\> 3  
> foo[4,-2] # =\> 23  
> foo[2, 2] # =\> 23
> 
> Conflating string reversal into setting the bounds of the substring  
> is a mistake, I’d say, particularly since there’s already a perfectly  
> reasonable String#reverse.
> 
> matthew smillie.

```
  I gather your opinion. And I'm sure that you know, there's always 

```

more than one way of achieving the same result. But having said that, it  
would be better if the program can reflect, and at the sametime when  
writing it, record accurately how a process actually works, assuming  
that you’re modelling a real life process. In other words, you can  
actually guess what’s acutally going on by just reading your codes.  
This is esp true and important in modelling business. Again, just for  
discussion purposes.

Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 2:08pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/5 "2006-06-11T14:08:32Z")

</div>

Robert K. wrote:

> 2006/6/11, Charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hi,
> > 
> > Just a logic question about substrings by position.
> > 
> > foo = “012345678”  
> > foo[5,1] #-\> “5”  
> > foo[-4,2] #-\> “56”  
> > foo[-3,2] = “” #-\> “0123458”
> 
> What version are you on?

1.8.4

You have probably missed out something, its foo[-3,2] = “” not  
foo[-3,2]

> > > foo = “012345678”  
> > > =\> “012345678”  
> > > foo[-3,2]  
> > > =\> “67”  
> > > RUBY\_VERSION  
> > > =\> “1.8.3”
> 
> Note also that you can use a range as well:
> 
> > > foo[-3…-1]  
> > > =\> “678”  
> > > foo[-3…-1]  
> > > =\> “67”
> 
> > Shouldn’t a negative indexing work as well? (Below are just examples,  
> > they currently don’t work) It will allow strings to be REVERSED easily,  
> > and thus doing something new.
> 
> Negative indexes do work already. You probably mean negative lengths.

Yes, you’re right. It just mean lengths with direction.

> > foo = “012345678”  
> > foo[4,1] #-\> 4  
> > foo[4,-1] #-\> 3
> > 
> > Is this making sense?
> 
> Dunno. I personally never missed this behavior. I’m not sure whether  
> there is much use for this and if it’s worth the effort. Note that the  
> implementation of String#[] becomes even more complex than it is right  
> now plus with the current design returned strings usually share the  
> buffer with the original. You cannot do that with implicit reversed  
> strings. I opt for a mild “no” but I’m not religious.

I just thought that if the current behaviour is so useful, then it’ll  
be equally useful to have an equal and opposite behaviour. It’s just  
unnatural that things can only go one direction. Just a thought.

> Kind regards
> 
> robert

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 12, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/6 "2006-06-12T14:12:10Z")

</div>

Robert K. wrote:

> 2006/6/11, Charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > What version are you on?
> > 
> > 1.8.4
> > 
> > You have probably missed out something, its foo[-3,2] = “” not  
> > foo[-3,2]
> 
> Right. But with assignment I would find it even more irritating to  
> have reversal - because the only thing that would make sense is to  
> apply the reversal to the assigned string - and this is extremely  
> unobvious.
> 
> > I just thought that if the current behaviour is so useful, then it’ll  
> > be equally useful to have an equal and opposite behaviour. It’s just  
> > unnatural that things can only go one direction. Just a thought.
> 
> I don’t subscribe to this general statement. Program code also flows  
> only one direction and it would be more than unnatural to have it both  
> ways.
> 
> Kind regards
> 
> robert

Thanks for your comments. I think you all got the gist of the idea, so I  
needn’t argue more. Personally, I think it has great potential for  
genome sequencing and genetic algorithm to name a few.

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [June 11, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/substrings-by-position/63051/7 "2006-06-11T22:41:39Z")

</div>

2006/6/11, Charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > What version are you on?
> 
> 1.8.4
> 
> You have probably missed out something, its foo[-3,2] = “” not  
> foo[-3,2]

Right. But with assignment I would find it even more irritating to  
have reversal - because the only thing that would make sense is to  
apply the reversal to the assigned string - and this is extremely  
unobvious.

> I just thought that if the current behaviour is so useful, then it’ll  
> be equally useful to have an equal and opposite behaviour. It’s just  
> unnatural that things can only go one direction. Just a thought.

I don’t subscribe to this general statement. Program code also flows  
only one direction and it would be more than unnatural to have it both  
ways.

Kind regards

robert
