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.

···

--
Posted via http://www.ruby-forum.com/.

Raj Singh 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]

···

--
Posted via http://www.ruby-forum.com/\.

Raj Singh 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

···

--
Posted via http://www.ruby-forum.com/\.

# 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

···

From: Raj Singh [mailto:neeraj.jsr@gmail.com]

Peña, Botp wrote:

From: Raj Singh [mailto:neeraj.jsr@gmail.com]
# 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

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/.

# 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

···

From: neeraj.jsr@gmail.com [mailto:neeraj.jsr@gmail.com]

Raj Singh 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]

···

--
Posted via http://www.ruby-forum.com/\.