Skip to content

Instantly share code, notes, and snippets.

View kristenmills's full-sized avatar

Kristen Mills kristenmills

View GitHub Profile
@kristenmills
kristenmills / split.rb
Created November 28, 2012 17:10
Splits a string into lines so that the number characters in a line is less than or equal to a specified length. Individual words will only be split if the word itself has more characters than can fit on a line.
def split string, length
split = Array.new
if string.length > length #if the string needs to be split
string_words = string.split(" ")
line = ""
string_words.each do |x|
if x.length > length #if the word needs to be split
#add the start of the word onto the first line (even if it has already started)
while line.length < length
line += x[0]