Guest User

Untitled

a guest
Apr 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. ## Chapter 8 ##
  2.  
  3. ### Building and sorting an array
  4.  
  5. puts "Enter your words"
  6. words = []
  7. while true
  8. word = gets.chomp
  9. if(word == '')
  10. break
  11. end
  12. words.push(word)
  13. end
  14. words.sort.each do |w|
  15. puts w
  16. end
  17.  
  18.  
  19. ### Table of contents, revisited
  20.  
  21. totalWidth = 60
  22. indexWidth = 3
  23. content = [["Getting Started", 1], ["Numbers", 9], ["Letters", 13]]
  24.  
  25.  
  26. puts "Table of Contents".center(totalWidth)
  27. puts
  28. chapterNumber = 1
  29. content.each do |chapter|
  30. section = "Chapter " + chapterNumber.to_s + ": " + chapter[0]
  31. index = "page " + chapter[1].to_s.rjust(indexWidth);
  32. if(section.length + index.length <= totalWidth)
  33. puts section.ljust(section.length) + index.rjust(totalWidth - section.length)
  34. else
  35. puts section.ljust(totalWidth)
  36. puts index.rjust(totalWidth)
  37. end
  38. chapterNumber += 1
  39. end
Add Comment
Please, Sign In to add comment