How is this split method working?

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.

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]

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

From: Raj S. [mailto:[email protected]]

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

if you look at the comments of this blog
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.

From: [email protected] [mailto:[email protected]]

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

Peña, Botp wrote:

From: Raj S. [mailto:[email protected]]

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

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]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs