# How is this split method working?

**URL:** <https://www.ruby-forum.com/t/how-is-this-split-method-working/138081>\
**Category:** Ruby\
**Created:** [May 20, 2008, 2:03am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081 "2008-05-20T02:03:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 2:03am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/1 "2008-05-20T02:03:40Z")

</div>

length = 20  
text = ’ This forum is connected to a mailing list that is read by  
thousands of people.’

text.split[0…length]

I looked at the split api but can’t understand how the above code is  
working.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 2:35am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/2 "2008-05-20T02:35:45Z")

</div>

Raj S. wrote:

> length = 20  
> text = ’ This forum is connected to a mailing list that is read by  
> thousands of people.’
> 
> text.split[0…length]
> 
> I looked at the split api but can’t understand how the above code is  
> working.

split() returns an array, and:

arr = [10, 20, 30, 40]  
p arr

slice = arr[0…2]  
p slice

–output:–  
[10, 20, 30, 40]  
[10, 20, 30]

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 5:46am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/3 "2008-05-20T05:46:41Z")

</div>

Raj S. wrote:

> length = 20  
> text = ’ This forum is connected to a mailing list that is read by  
> thousands of people.’
> 
> text.split[0…length]
> 
> I looked at the split api but can’t understand how the above code is  
> working.

Its not specific how you want to split your string but may be

split\_text = split(’’)

will split the string of characters like,

T  
h  
i  
s

f  
o  
…

so samething like

split\_text = split(’ ')

withh split the string into an array of strings like

This  
forum  
is  
connected  
to  
a  
mailing  
list  
…

so if you give your specifications clearly we are ready to help you Raj  
singh…

— Jose Martin

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 6:20am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/4 "2008-05-20T06:20:53Z")

</div>

From: Raj S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# length = 20

# text = ’ This forum is connected to a mailing list that is read by

# thousands of people.’

# text.split[0…length]

# I looked at the split api but can’t understand how the above code is

# working.

Hi Raj,

u didn’t show what you wanted.

so (i guess :), maybe you want string#[] or string#slice instead,

length=20  
#=\> 20

text = ’ This forum is connected to a mailing list that is read by  
thousands of people’  
#=\> " This forum is connected to a mailing list that is read by  
thousands of people"

text[0,length]  
#=\> " This forum is conne"

text[0…length-1]  
#=\> " This forum is conne"

text[length…-1]  
#=\> “cted to a mailing list that is read by thousands of people”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 7:33am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/5 "2008-05-20T07:33:38Z")

</div>

if you look at the comments of this blog  
[http://daniel.collectiveidea.com/blog/2007/7/10/a-prettier-truncate-helper](http://daniel.collectiveidea.com/blog/2007/7/10/a-prettier-truncate-helper)  
you will find following code.

def truncate\_words(text, length = 10, separator = ’ ', truncate\_string =  
‘…’)  
’ ’ if text.nil?

truncated\_text = text.split[0…length].join(separator)

if(truncated\_text == text)  
text  
else  
truncated\_text + ’ ’ + truncate\_string  
end  
end

I had never seen text.split[0…length] before. What’s happening here. I  
always saw split being passed an argument but never an array.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 8:53am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/6 "2008-05-20T08:53:16Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I had never seen text.split[0…length] before. What’s

# happening here. I always saw split being passed an argument

# but never an array.

an array was \*never passed.

try playing w irb,

text  
#=\> " This forum is connected to a mailing list that is read by  
thousands of people"

text.split  
#=\> [“This”, “forum”, “is”, “connected”, “to”, “a”, “mailing”, “list”,  
“that”, “is”, “read”, “by”, “thousands”, “of”, “people”]

text.split[0…5]  
#=\> [“This”, “forum”, “is”, “connected”, “to”, “a”]

text.split[0…5].join " "  
#=\> “This forum is connected to a”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 6:30am UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/7 "2008-05-20T06:30:35Z")

</div>

PeÃ±a, Botp wrote:

> From: Raj S. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # length = 20
> 
> # text = ’ This forum is connected to a mailing list that is read by
> 
> # thousands of people.’
> 
> # text.split[0…length]
> 
> # I looked at the split api but can’t understand how the above code is
> 
> # working.
> 
> Hi Raj,
> 
> u didn’t show what you wanted.
> 
> so (i guess :), maybe you want string#[] or string#slice instead,
> 
> length=20  
> #=\> 20
> 
> text = ’ This forum is connected to a mailing list that is read by  
> thousands of people’  
> #=\> " This forum is connected to a mailing list that is read by  
> thousands of people"
> 
> text[0,length]  
> #=\> " This forum is conne"
> 
> text[0…length-1]  
> #=\> " This forum is conne"
> 
> text[length…-1]  
> #=\> “cted to a mailing list that is read by thousands of people”
> 
> kind regards -botp

yes i agree with your comments.

raj you did not specified what you need clearly but hope the above would  
help you a lot in your work.

this is the thing you expected?

ready to do it for you but need specifications clearly.

– jose martin

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 20, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/how-is-this-split-method-working/138081/8 "2008-05-20T20:07:23Z")

</div>

Raj S. wrote:

> I had never seen text.split[0…length] before. What’s happening here. I  
> always saw split being passed an argument but never an array.

The code you originally posted:

> result = text.split[0…length]

is equivalent to:

arr = text.split  
result = arr[0…length]
