I think I’m going crazy. I tried to write a fairly simple program to

test ljust, center and rjust. But I cannot make the page numbers of my

table of contents line up. I tried to account for the string length of

the chapter and topics; I’ve tried a lot of things and I just cannot get

the page numbers to line up. If anyone knows how to do it please let me

know. I don’t think I’ll be able to go to bed without figuring it out.

Here is my latest code and output:

////////// code //////////////

class TableOfContents

attr_accessor :chapters, :topics, :pages

end

toc = TableOfContents.new

toc.chapters = [“Chapter 1”, “Chapter 2”, “Chapter 3”]

toc.topics = [“Numbers”, “Letters”, “Variables”]

toc.pages = [“Page 1”, “Page 57”, “Page 117”]

lineWidth = 20

3.times do |x|

offset = toc.chapters[x] + ": " + toc.topics[x]

offset = 40 - offset.length

puts toc.chapters[x] + ": " + toc.topics[x] +

toc.pages[x].rjust(lineWidth + offset + (10 - toc.pages[x].length))

end

//////////// output /////////////////

Chapter 1: Numbers Page 1

Chapter 2: Letters Page 57

Chapter 3: Variables Page 117

!! Using this line rather than the above line 14 gives this

new line 14:

puts toc.chapters[x] + ": " + toc.topics[x] +

toc.pages[x].rjust(lineWidth)

new output:

Chapter 1: Numbers Page 1

Chapter 2: Letters Page 57

Chapter 3: Variables Page 117

Thanks