# Php's chunk\_split alternative in ruby

**URL:** <https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090>\
**Category:** Ruby\
**Created:** [April 27, 2007, 11:44am UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090 "2007-04-27T11:44:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 11:44am UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/1 "2007-04-27T11:44:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 11:58am UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/2 "2007-04-27T11:58:20Z")

</div>

Mister T. 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…).

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/3 "2007-04-27T12:00:20Z")

</div>

On Apr 27, 4:44 am, Mister T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is it in ruby?
> 
> Thank you in advance!
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

long\_string = “some long long long string”  
p long\_string.scan(/.{3}/)

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/4 "2007-04-27T13:03:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 12:00pm UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/5 "2007-04-27T12:00:34Z")

</div>

Alex Y. wrote:

> Mister T. 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 😉

---

<div class="post-metadata">

**Author:** ![webdevotion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/48db29/32.png) [@webdevotion](https://www.ruby-forum.com/u/webdevotion)\
**Post date:** [April 27, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/phps-chunk-split-alternative-in-ruby/93090/6 "2007-04-27T13:07:06Z")

</div>

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
