[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rjust alignment

Ross

3/9/2007 5:46:00 AM

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

--
Posted via http://www.ruby-....

2 Answers

Ross

3/9/2007 6:48:00 AM

0

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.

--
Posted via http://www.ruby-....

John Joyce

3/9/2007 11:14:00 AM

0

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 Mar 9, 2007, at 2:45 PM, Ross wrote:

> 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
>
> --
> Posted via http://www.ruby-....
>