How is this split method working?

R

Raj Singh

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.
 
7

7stud --

Raj said:
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]
 
D

dare ruby

Raj said:
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
 
P

Peña, Botp

RnJvbTogUmFqIFNpbmdoIFttYWlsdG86bmVlcmFqLmpzckBnbWFpbC5jb21dIA0KIyBsZW5ndGgg
PSAyMA0KIyB0ZXh0ID0gJyBUaGlzIGZvcnVtIGlzIGNvbm5lY3RlZCB0byBhIG1haWxpbmcgbGlz
dCB0aGF0IGlzIHJlYWQgYnkNCiMgdGhvdXNhbmRzIG9mIHBlb3BsZS4nDQojIHRleHQuc3BsaXRb
MC4ubGVuZ3RoXQ0KIyBJIGxvb2tlZCBhdCB0aGUgc3BsaXQgYXBpIGJ1dCBjYW4ndCB1bmRlcnN0
YW5kIGhvdyB0aGUgYWJvdmUgY29kZSBpcw0KIyB3b3JraW5nLg0KDQpIaSBSYWosDQoNCnUgZGlk
bid0IHNob3cgd2hhdCB5b3Ugd2FudGVkLg0KDQpzbyAoaSBndWVzcyA6KSwgbWF5YmUgeW91IHdh
bnQgc3RyaW5nI1tdIG9yIHN0cmluZyNzbGljZSBpbnN0ZWFkLA0KDQpsZW5ndGg9MjANCiM9PiAy
MA0KDQp0ZXh0ID0gJyBUaGlzIGZvcnVtIGlzIGNvbm5lY3RlZCB0byBhIG1haWxpbmcgbGlzdCB0
aGF0IGlzIHJlYWQgYnkgdGhvdXNhbmRzIG9mIHBlb3BsZScNCiM9PiAiIFRoaXMgZm9ydW0gaXMg
Y29ubmVjdGVkIHRvIGEgbWFpbGluZyBsaXN0IHRoYXQgaXMgcmVhZCBieSB0aG91c2FuZHMgb2Yg
cGVvcGxlIg0KDQp0ZXh0WzAsbGVuZ3RoXQ0KIz0+ICIgVGhpcyBmb3J1bSBpcyBjb25uZSINCg0K
dGV4dFswLi5sZW5ndGgtMV0NCiM9PiAiIFRoaXMgZm9ydW0gaXMgY29ubmUiDQoNCnRleHRbbGVu
Z3RoLi4tMV0NCiM9PiAiY3RlZCB0byBhIG1haWxpbmcgbGlzdCB0aGF0IGlzIHJlYWQgYnkgdGhv
dXNhbmRzIG9mIHBlb3BsZSINCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=
 
D

dare ruby

From: Raj Singh [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
 
R

Raj Singh

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.
 
P

Peña, Botp

RnJvbTogbmVlcmFqLmpzckBnbWFpbC5jb20gW21haWx0bzpuZWVyYWouanNyQGdtYWlsLmNvbV0g
DQojIEkgaGFkIG5ldmVyIHNlZW4gdGV4dC5zcGxpdFswLi5sZW5ndGhdIGJlZm9yZS4gV2hhdCdz
IA0KIyBoYXBwZW5pbmcgaGVyZS4gSSBhbHdheXMgc2F3ICBzcGxpdCBiZWluZyBwYXNzZWQgYW4g
YXJndW1lbnQgDQojIGJ1dCBuZXZlciBhbiBhcnJheS4NCg0KYW4gYXJyYXkgd2FzICpuZXZlciBw
YXNzZWQuDQoNCnRyeSBwbGF5aW5nIHcgaXJiLA0KDQp0ZXh0DQojPT4gIiBUaGlzIGZvcnVtIGlz
IGNvbm5lY3RlZCB0byBhIG1haWxpbmcgbGlzdCB0aGF0IGlzIHJlYWQgYnkgdGhvdXNhbmRzIG9m
IHBlb3BsZSINCg0KdGV4dC5zcGxpdA0KIz0+IFsiVGhpcyIsICJmb3J1bSIsICJpcyIsICJjb25u
ZWN0ZWQiLCAidG8iLCAiYSIsICJtYWlsaW5nIiwgImxpc3QiLCAidGhhdCIsICJpcyIsICJyZWFk
IiwgImJ5IiwgInRob3VzYW5kcyIsICJvZiIsICJwZW9wbGUiXQ0KDQp0ZXh0LnNwbGl0WzAuLjVd
DQojPT4gWyJUaGlzIiwgImZvcnVtIiwgImlzIiwgImNvbm5lY3RlZCIsICJ0byIsICJhIl0NCg0K
dGV4dC5zcGxpdFswLi41XS5qb2luICIgIg0KIz0+ICJUaGlzIGZvcnVtIGlzIGNvbm5lY3RlZCB0
byBhIg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
7

7stud --

Raj said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top