DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Split A String Into Groups Of 4

03.22.2010
| 4608 views |
  • submit to reddit
        
s = "ewerweouwroiuwe sdffd sdfds sfdsdf sdf"
s.scan(/.{1,4}/)
#=> ["ewer", "weou", "wroi", "uwe ", "sdff", "d sd", "fds ", "sfds", "df s", "df"]

# or 
s.scan(/./).each_slice(4).map(&:join)