title: Split a string into chunks of specified size without breaking words
body: <p>I need to split a string into chunks according to a specific size. I cannot break words between chunks, so I need to catch when adding the next word will go over chunk size and start the next one (it's ok if a chunk is less than specified size).</p>
  
  <p>Here is my working code, but I would like to find a more elegant way to do this.</p>
  
  <pre><code>def split_into_chunks_by_size(chunk_size, string)
    string_split_into_chunks = [""]
    string.split(" ").each do |word|
      if (string_split_into_chunks[-1].length + 1 + word.length &gt; chunk_size)
        string_split_into_chunks &lt;&lt; word
      else
        string_split_into_chunks[-1] &lt;&lt; " " + word
      end
    end
    return string_split_into_chunks
  end
  </code></pre>