[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

php's chunk_split alternative in ruby

roi

4/27/2007 9:45:00 AM

hello,
how could I chunk_split string

mylongstring = "some long long long string"

into smaller strings of length 3.

In PHP it is

chunk_split(mylongstring,3);

What is it in ruby?

Thank you in advance!

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

5 Answers

William James

4/27/2007 9:57:00 AM

0

On Apr 27, 4:44 am, Mister Twister <kroni...@gmail.com> wrote:
> hello,
> how could I chunk_split string
>
> mylongstring = "some long long long string"
>
> into smaller strings of length 3.
>
> In PHP it is
>
> chunk_split(mylongstring,3);
>
> What is it in ruby?
>
> Thank you in advance!
>
> --
> Posted viahttp://www.ruby-....

long_string = "some long long long string"
p long_string.scan(/.{3}/)

Alex Young

4/27/2007 9:57:00 AM

0

Mister Twister wrote:
> hello,
> how could I chunk_split string
>
> mylongstring = "some long long long string"
>
> into smaller strings of length 3.
>
> In PHP it is
>
> chunk_split(mylongstring,3);
>
irb(main):005:0> "some long long long string".scan(/.{1,3}/)
=> ["som", "e l", "ong", " lo", "ng ", "lon", "g s", "tri", "ng"]

Like that? If not, you'll need to give an example of the output you're
trying to achieve (which is generally a good idea anyway...).

--
Alex


Firstname Secondname

4/27/2007 10:01:00 AM

0

Alex Young wrote:
> Mister Twister wrote:
>>
> irb(main):005:0> "some long long long string".scan(/.{1,3}/)
> => ["som", "e l", "ong", " lo", "ng ", "lon", "g s", "tri", "ng"]
>
> Like that? If not, you'll need to give an example of the output you're
> trying to achieve (which is generally a good idea anyway...).

Exactly like that :)

Thank you ;)

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

Peña, Botp

4/27/2007 10:55:00 AM

0

On Behalf Of Firstname Secondname:
# Exactly like that :)

Ah, do not forget, in ruby land, you can be flexible :)

C:\family\ruby>cat test.rb
class String
alias old_split split
def split arg1=' ', *args
case arg1
when Integer
self.scan(/.{1,#{arg1}}/)
else
old_split arg1, *args
end #case
end #def
end #class

p "this is a test string. okay?".split
p "this is a test string. okay?".split(/i/)
p "this is a test string. okay?".split(3)

C:\family\ruby>ruby test.rb
["this", "is", "a", "test", "string.", "okay?"]
["th", "s ", "s a test str", "ng. okay?"]
["thi", "s i", "s a", " te", "st ", "str", "ing", ". o", "kay", "?"]

Is that ok?
kind regards -botp

Firstname Secondname

4/27/2007 11:07:00 AM

0

class String
alias old_split split

cool :)

Peña, Botp wrote:
> On Behalf Of Firstname Secondname:
> # Exactly like that :)
>
> Ah, do not forget, in ruby land, you can be flexible :)
>
> C:\family\ruby>cat test.rb
> class String
> alias old_split split
> def split arg1=' ', *args
> case arg1
> when Integer
> self.scan(/.{1,#{arg1}}/)
> else
> old_split arg1, *args
> end #case
> end #def
> end #class
>
> p "this is a test string. okay?".split
> p "this is a test string. okay?".split(/i/)
> p "this is a test string. okay?".split(3)
>
> C:\family\ruby>ruby test.rb
> ["this", "is", "a", "test", "string.", "okay?"]
> ["th", "s ", "s a test str", "ng. okay?"]
> ["thi", "s i", "s a", " te", "st ", "str", "ing", ". o", "kay", "?"]
>
> Is that ok?
> kind regards -botp


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