[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How is this split method working?

Raj Singh

5/20/2008 12:04:00 AM

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

7 Answers

7stud --

5/20/2008 12:36:00 AM

0

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

dare ruby

5/20/2008 3:47:00 AM

0

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

Peña, Botp

5/20/2008 4:20:00 AM

0

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

dare ruby

5/20/2008 4:31:00 AM

0

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

Raj Singh

5/20/2008 5:34:00 AM

0

if you look at the comments of this blog
http://daniel.collectiveidea.com/blog/2007/7/10/a-prettier-trunc...
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-....

Peña, Botp

5/20/2008 6:52:00 AM

0

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

7stud --

5/20/2008 6:07:00 PM

0

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