# Strings - foo\[0,0\] vs.. foo\[-1,0\]?

**URL:** <https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164>\
**Category:** Ruby\
**Created:** [January 6, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164 "2010-01-06T17:21:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [January 6, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164/1 "2010-01-06T17:21:06Z")

</div>

Since some time I have used this to prepend something to a string:

string = ‘bc’  
string[0,0] = ‘a’

Resulting in:

‘abc’

Then I recently wanted to append something to a string.  
Of course there exists the \<\< method already

string = ‘ab’  
string \<\< ‘c’

But there does not seem to be a way via []  
Using

string[-1,0] = ‘c’

will add at the position before. Is it true that with the  
[] method on string one can not append just one character to it?

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [January 6, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164/2 "2010-01-06T18:24:29Z")

</div>

On Thu, 7 Jan 2010, Marc H. wrote:

> Of course there exists the \<\< method already  
> [] method on string one can not append just one character to it?  
> string[string.size,0]=‘c’ works

But I think string \<\< ‘c’ is clearer

YMMV

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [March 1, 2010, 1:52am UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164/3 "2010-03-01T01:52:40Z")

</div>

Matthew K. Williams wrote:

> On Thu, 7 Jan 2010, Marc H. wrote:
> 
> > Of course there exists the \<\< method already  
> > [] method on string one can not append just one character to it?  
> > string[string.size,0]=‘c’ works
> 
> But I think string \<\< ‘c’ is clearer
> 
> YMMV

Thanks.

I agree with you, string \<\< ‘c’ is a lot clearer.

Can someone explain the code to me? I am that thickhead. 😃

This code works:

string = ‘ab’  
string[2, 0] = ‘c’

But why does that work?

And via -1, 0 it would not work right?

I think I am confused why -1, 0 does not work. (My poor brain gives up  
too easily…)

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [April 19, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164/4 "2010-04-19T16:31:34Z")

</div>

I think that finally helped, thanks.

It is a lot easier for my brain if I use the word _marker_ as well. 🙂

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [March 1, 2010, 11:19am UTC](https://www.ruby-forum.com/t/strings-foo-0-0-vs-foo-1-0/181164/5 "2010-03-01T11:19:16Z")

</div>

string[i,1] gives you the char at index i,

string[i,0] gives you not the char but some kind of a marker just  
_before_ that char.

So string[i,1]= ‘X’ overwrites that char, while

string[i,0] = ‘X’ inserts before it.

string[-1,1] gives you the last char of string, accordingly

string[-1,0] gives you that marker just _before_ the last char.

You would have to do something like string[-0,0] to get _behind_ the  
last char if you want to count from the end of the string, which is not  
possible so you have to count from the beginning: string[string.size,0]
