# Rjust alignment

**URL:** <https://www.ruby-forum.com/t/rjust-alignment/88252>\
**Category:** Ruby\
**Created:** [March 9, 2007, 6:45am UTC](https://www.ruby-forum.com/t/rjust-alignment/88252 "2007-03-09T06:45:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 6:45am UTC](https://www.ruby-forum.com/t/rjust-alignment/88252/1 "2007-03-09T06:45:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 7:47am UTC](https://www.ruby-forum.com/t/rjust-alignment/88252/2 "2007-03-09T07:47:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/rjust-alignment/88252/3 "2007-03-09T12:15:40Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 9, 2007, 7:23am UTC](https://www.ruby-forum.com/t/rjust-alignment/88252/4 "2007-03-09T07:23:09Z")

</div>

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 🙂

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
