Guest User

Untitled

a guest
Apr 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. ## Chapter 8 ##
  2.  
  3. ### Building and sorting an array
  4. words_array = []
  5. puts 'Write one word per line, enter an empty line to finish'
  6. while true
  7. word = gets.chomp
  8. if word == ""
  9. break
  10. end
  11. if word.include? ' '
  12. puts "Just one word per line please."
  13. next
  14. end
  15. words_array.push(word)
  16. end
  17. puts 'Sorted words: '
  18. puts words_array.sort()
  19.  
  20. ### Table of contents, revisited
  21. LINE_WIDTH = 55
  22. HALF_LINE_WIDTH = LINE_WIDTH/2
  23.  
  24. table_elements = [["Getting Started"," 1"],["Numbers"," 9"],["Letters","13"]]
  25.  
  26. puts 'Table of Contents'.center(LINE_WIDTH)
  27. puts ''
  28.  
  29. i = 0
  30. table_elements.each do |element|
  31. chapter = "Chapter " + i.to_s + ": " + element[0]
  32. page = "page " + element[1]
  33. puts chapter.ljust(HALF_LINE_WIDTH) + page.rjust(HALF_LINE_WIDTH)
  34. i += 1
  35. end
Add Comment
Please, Sign In to add comment