I have been looking for an elegant and efficient way to chunk a string into substrings of a given length in Ruby.
So far, the best I could come up with is this:
I think this is the most efficient solution if you know your string is a multiple of chunk size
def chunk(string, size)
(string.length / size).times.collect { |i| string[i * size, size] }
end
and for parts
def parts(string, count)
size = string.length / count
count.times.collect { |i| string[i * size, size] }
end