Rjust alignment

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

Thanks. I am using FreeRIDE and the output shows up differently, but if
I copy and paste the output to a text file it is lined up like it is
supposed to be.

Thanks for the suggestions.

Maybe you’re looking at the numbers wrong.
You could roll your own pretty easily.
Some pseudo code:

TotalWidth = x
TotalWidthForLine = TotalWidth - ( ChapterTitleWidth + PageNumberWidth )

On Behalf Of Ross:

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

relax.

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

toc.pages[x].rjust(offset+10) #<-try this first

suggest to take a second look to #just… it rjusts the whole string.
subtraction not needed :slight_smile:

sample output should then be,

C:\family\ruby>cat -n test.rb
1 class TableOfContents
2 attr_accessor :chapters, :topics, :pages
3 end
4
5 toc = TableOfContents.new
6 toc.chapters = [“Chapter 1”, “Chapter 2”, “Chapter 3”]
7 toc.topics = [“Numbers”, “Letters”, “Variables”]
8 toc.pages = [“Page 1”, “Page 57”, “Page 117”]
9
10 lineWidth = 20
11 3.times do |x|
12 offset = toc.chapters[x] + ": " + toc.topics[x]
13 offset = 40 - offset.length
14 puts toc.chapters[x] + ": " + toc.topics[x] +
15 toc.pages[x].rjust(offset)
16 #toc.pages[x].rjust(offset+10,“x”)
17 end

C:\family\ruby>ruby test.rb
Chapter 1: Numbers Page 1
Chapter 2: Letters Page 57
Chapter 3: Variables Page 117

C:\family\ruby>

btw, i’d suggest you put “Chapter”, and “Page” as headers, like so,

Chapter Page
1: Numbers 1
2: Letters 57
3: Variables 117

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs