Skip to content

Instantly share code, notes, and snippets.

@rklemme
Forked from thehack/phrase_splitter.rb
Created June 6, 2011 14:12
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 rklemme/1010337 to your computer and use it in GitHub Desktop.
Save rklemme/1010337 to your computer and use it in GitHub Desktop.
(fork) I'm trying to split a phrase into 6 or less rows of 12 or less letters each.
LEN = 60
LIMIT = 12
phrase = nil
begin
puts "Enter phrase. #{LEN} characters or less."
phrase = gets.chomp
end while phrase.length > LEN
lines = []
phrase.downcase.scan /[a-z]+/ do |word|
if lines.empty? || lines.last.length + 1 + word.length > LIMIT
lines << word.dup
else
lines.last << " " << word
end
end
puts lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment