Skip to content

Instantly share code, notes, and snippets.

@kirill-fedyanin
Created December 4, 2016 11:36
Show Gist options
  • Save kirill-fedyanin/63297de764d75ef0954291c5f1d078b9 to your computer and use it in GitHub Desktop.
Save kirill-fedyanin/63297de764d75ef0954291c5f1d078b9 to your computer and use it in GitHub Desktop.
def justify(text, width)
text.split.reduce([[]]) do |lines, word|
line = lines.last
if (spaces = width - line.join(' ').length) > word.length
line << word
else
spaces.times { |i| line[i % (line.count - 1)] += ' ' } if line.count > 1
lines << [word]
end
lines
end.map { |line| line.join(' ') }.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment