Exercises from Chapter 8

Building and Sorting an Array

How you could do it:

puts ​'Give me some words, and I will sort them:'
words = []
while​ true
word = gets.chomp
if​ word == ​''
break
end
words.push word
end
puts ​'Sweet! Here they are, sorted:'
puts words.sort
<= Give me some words, and I will sort them:
=> banana
=> apple
=> cherry
=> 
<= Sweet! Here they are, sorted:
 apple
 banana
 cherry

How I would do it:

puts ​'Give me some words, and I will sort them:'
words = []
while​ true
word = gets.chomp
break​ ​if​ word.empty?
words << word
end
puts ​'Sweet! Here they are, sorted:'
puts words.sort
<= Give me some words, and I will sort them:
=> banana
=> apple
=> cherry
=> 
<= Sweet! Here they are, sorted:
 apple
 banana
 cherry

Table of Contents, Revisited

How you could do it:

title = ​'Table of Contents'
chapters = [[​'Getting Started'​, 1],
[​'Numbers'​, 9],
[​'Letters'​, 13]]
puts title.center(50)
puts
chap_num = 1
chapters.each ​do​ |chap|
name = chap[0]
page = chap[1]
beginning = ​'Chapter '​ + chap_num.to_s + ​': '​ + name
ending = ​'page '​ + page.to_s
puts beginning.ljust(30) + ending.rjust(20)
chap_num = chap_num + 1
end
Table of Contents
Chapter 1: Getting Started page 1
Chapter 2: Numbers page 9
Chapter 3: Letters page 13

How I would do it:

title = ​'Table of Contents'
chapters = [[​'Getting Started'​, 1],
[​'Numbers'​, 9],
[​'Letters'​, 13]]
puts title.center(50)
puts
chapters.each_with_index ​do​ |chap, idx|
name, page = chap
chap_num = idx + 1
beginning = ​"Chapter ​#{chap_num}​: ​#{name}​"
ending = ​"page ​#{page}​"
puts beginning.ljust(30) + ending.rjust(20)
end
Table of Contents
Chapter 1: Getting Started page 1
Chapter 2: Numbers page 9
Chapter 3: Letters page 13
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.238.31