Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Chapter 8 ##
- ### Building and sorting an array
- words_array = []
- puts 'Write one word per line, enter an empty line to finish'
- while true
- word = gets.chomp
- if word == ""
- break
- end
- if word.include? ' '
- puts "Just one word per line please."
- next
- end
- words_array.push(word)
- end
- puts 'Sorted words: '
- puts words_array.sort()
- ### Table of contents, revisited
- LINE_WIDTH = 55
- HALF_LINE_WIDTH = LINE_WIDTH/2
- table_elements = [["Getting Started"," 1"],["Numbers"," 9"],["Letters","13"]]
- puts 'Table of Contents'.center(LINE_WIDTH)
- puts ''
- i = 0
- table_elements.each do |element|
- chapter = "Chapter " + i.to_s + ": " + element[0]
- page = "page " + element[1]
- puts chapter.ljust(HALF_LINE_WIDTH) + page.rjust(HALF_LINE_WIDTH)
- i += 1
- end
Add Comment
Please, Sign In to add comment