php's chunk_split alternative in ruby

M

Mister Twister

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!
 
W

William James

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!

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

Alex Young

Mister said:
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...).
 
F

Firstname Secondname

Alex said:
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 ;)
 
P

Peña, Botp

T24gQmVoYWxmIE9mIEZpcnN0bmFtZSBTZWNvbmRuYW1lOg0KIyBFeGFjdGx5IGxpa2UgdGhhdCA6
KQ0KDQpBaCwgZG8gbm90IGZvcmdldCwgaW4gcnVieSBsYW5kLCB5b3UgY2FuIGJlIGZsZXhpYmxl
IDopDQoNCkM6XGZhbWlseVxydWJ5PmNhdCAgdGVzdC5yYg0KY2xhc3MgU3RyaW5nDQogICBhbGlh
cyBvbGRfc3BsaXQgc3BsaXQNCiAgIGRlZiBzcGxpdCBhcmcxPScgJywgKmFyZ3MNCiAgICAgIGNh
c2UgYXJnMQ0KICAgICAgICAgd2hlbiBJbnRlZ2VyDQogICAgICAgICAgICBzZWxmLnNjYW4oLy57
MSwje2FyZzF9fS8pDQogICAgICAgICBlbHNlDQogICAgICAgICAgICBvbGRfc3BsaXQgYXJnMSwg
KmFyZ3MNCiAgICAgIGVuZCAjY2FzZQ0KICAgZW5kICNkZWYNCmVuZCAjY2xhc3MNCg0KcCAidGhp
cyBpcyBhIHRlc3Qgc3RyaW5nLiBva2F5PyIuc3BsaXQNCnAgInRoaXMgaXMgYSB0ZXN0IHN0cmlu
Zy4gb2theT8iLnNwbGl0KC9pLykNCnAgInRoaXMgaXMgYSB0ZXN0IHN0cmluZy4gb2theT8iLnNw
bGl0KDMpDQoNCkM6XGZhbWlseVxydWJ5PnJ1YnkgdGVzdC5yYg0KWyJ0aGlzIiwgImlzIiwgImEi
LCAidGVzdCIsICJzdHJpbmcuIiwgIm9rYXk/Il0NClsidGgiLCAicyAiLCAicyBhIHRlc3Qgc3Ry
IiwgIm5nLiBva2F5PyJdDQpbInRoaSIsICJzIGkiLCAicyBhIiwgIiB0ZSIsICJzdCAiLCAic3Ry
IiwgImluZyIsICIuIG8iLCAia2F5IiwgIj8iXQ0KDQpJcyB0aGF0IG9rPw0Ka2luZCByZWdhcmRz
IC1ib3RwDQo=
 
F

Firstname Secondname

class String
alias old_split split

cool :)

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top