Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Chapter 8 ##
- ### Building and sorting an array
- puts "Enter your words"
- words = []
- while true
- word = gets.chomp
- if(word == '')
- break
- end
- words.push(word)
- end
- words.sort.each do |w|
- puts w
- end
- ### Table of contents, revisited
- totalWidth = 60
- indexWidth = 3
- content = [["Getting Started", 1], ["Numbers", 9], ["Letters", 13]]
- puts "Table of Contents".center(totalWidth)
- puts
- chapterNumber = 1
- content.each do |chapter|
- section = "Chapter " + chapterNumber.to_s + ": " + chapter[0]
- index = "page " + chapter[1].to_s.rjust(indexWidth);
- if(section.length + index.length <= totalWidth)
- puts section.ljust(section.length) + index.rjust(totalWidth - section.length)
- else
- puts section.ljust(totalWidth)
- puts index.rjust(totalWidth)
- end
- chapterNumber += 1
- end
Add Comment
Please, Sign In to add comment