# How does slice work?

**URL:** <https://www.ruby-forum.com/t/how-does-slice-work/203213>\
**Category:** Ruby\
**Created:** [February 15, 2011, 5:46pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213 "2011-02-15T17:46:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 15, 2011, 5:46pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/1 "2011-02-15T17:46:41Z")

</div>

How does the slice method work? If I want to write:

x.slice(parameter 1, parameter 2)

what do the parameters specify in terms of what part of x (say it’s a  
sentence) the program slices?

Thanks!

---

<div class="post-metadata">

**Author:** ![gabaluschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b3f665/32.png) [@gabaluschi](https://www.ruby-forum.com/u/gabaluschi)\
**Post date:** [February 15, 2011, 5:52pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/2 "2011-02-15T17:52:18Z")

</div>

On Tue, Feb 15, 2011 at 5:46 PM, Gaba L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How does the slice method work? If I want to write:
> 
> x.slice(parameter 1, parameter 2)
> 
> what do the parameters specify in terms of what part of x (say it’s a  
> sentence) the program slices?

ri String#slice Array#slice

robert

---

<div class="post-metadata">

**Author:** ![gabaluschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b3f665/32.png) [@gabaluschi](https://www.ruby-forum.com/u/gabaluschi)\
**Post date:** [February 15, 2011, 6:15pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/3 "2011-02-15T18:15:33Z")

</div>

Thanks - I’m still a little bit confused. Where does the slice  
begin/stop slicing?

---

<div class="post-metadata">

**Author:** ![gabaluschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b3f665/32.png) [@gabaluschi](https://www.ruby-forum.com/u/gabaluschi)\
**Post date:** [February 15, 2011, 6:29pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/4 "2011-02-15T18:29:55Z")

</div>

It selects the number of elements given by the second parameter,  
starting at the index given by the first parameter.

On Tue, Feb 15, 2011 at 11:15 AM, Gaba L.

---

<div class="post-metadata">

**Author:** ![gabaluschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b3f665/32.png) [@gabaluschi](https://www.ruby-forum.com/u/gabaluschi)\
**Post date:** [February 15, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/5 "2011-02-15T18:51:40Z")

</div>

Thanks - very clear!

---

<div class="post-metadata">

**Author:** ![gabaluschi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b3f665/32.png) [@gabaluschi](https://www.ruby-forum.com/u/gabaluschi)\
**Post date:** [February 15, 2011, 6:31pm UTC](https://www.ruby-forum.com/t/how-does-slice-work/203213/6 "2011-02-15T18:31:10Z")

</div>

You pick a starting point to slice and then a number of items to  
slice. For example, if you write:

“0123456789”.slice(i, size)

then you will slice starting after the `i`-th character, and for  
`size` more characters, or until the end of the sequence, whichever  
comes first. For example,

“0123456789”.slice(5, 3)

will yield “567”. We start at “5” because the i = 5th character is  
“4”, and “5” is right after that. Then we take size = 3 characters in  
all, yielding “567”.

By contrast, if you wrote,

“abcdefgh”.slice(5, 10)

you will get “fgh”, because we can’t take any more characters after  
“h” from the sequence.

## ~ jf

John F.  
Principal Consultant, BitsBuilder  
LI: [http://www.linkedin.com/in/johnxf](http://www.linkedin.com/in/johnxf)  
SO: [http://stackoverflow.com/users/75170/](http://stackoverflow.com/users/75170/)
