Skip to content

Instantly share code, notes, and snippets.

@shamil614
Created October 12, 2012 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamil614/3880427 to your computer and use it in GitHub Desktop.
Save shamil614/3880427 to your computer and use it in GitHub Desktop.
Split up a string by words using a character limit.
class String
def chunk_by_words(char_count)
words = self.split
s1_len = 0
word_count = 0
i = 0
while (i <= words.count-1)
s1_len += words[i].length
word_count += 1
i+=1
break if s1_len >= char_count
end
[words[0..word_count-1].join(' '), words[word_count..words.count].join(' ')]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment