特定のサイズに従って、文字列をチャンクに分割する必要があります。チャンク間で単語を分割することはできないため、次の単語を追加するとチャンク サイズを超えて次の単語を開始するタイミングをキャッチする必要があります (チャンクが指定されたサイズよりも小さくても問題ありません)。
これが私の作業コードですが、これを行うためのよりエレガントな方法を見つけたいと思います。
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 > chunk_size)
string_split_into_chunks << word
else
string_split_into_chunks[-1] << " " + word
end
end
return string_split_into_chunks
end