Skip to content

Instantly share code, notes, and snippets.

@thehack
Created June 6, 2011 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thehack/1010191 to your computer and use it in GitHub Desktop.
Save thehack/1010191 to your computer and use it in GitHub Desktop.
I'm trying to split a phrase into 6 or less rows of 12 or less letters each.
#!/usr/bin/ruby
puts "Enter phrase. 60 characters or less."
phrase = gets.chomp
words = phrase.downcase.gsub(/[^a-z ]/, "").split(" ")
word_count = words.length
word_lengths = []
words.each { |word| word_lengths << word.length }
# split the phrase into up to six lines, each no longer than twelve characters long.
line1, line2, line3, line4, line5, line6 = [], [], [], [], [], []
for line in [line1, line2, line3, line4, line5, line6]
if (line << words.shift).join(" ").length <= 12
line << words.shift
end
end
puts "line1: " + line1.join(" ")
puts "line2: " + line2.join(" ")
puts "line3: " + line3.join(" ")
puts "line4: " + line4.join(" ")
puts "line5: " + line5.join(" ")
puts "line6: " + line6.join(" ")
#I'm worried this is a bit hackish, especially the while loop. Is there a better way?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment