# Using two justifications on the same line

**URL:** <https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764>\
**Category:** Ruby\
**Created:** [November 21, 2006, 12:08am UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764 "2006-11-21T00:08:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 21, 2006, 12:08am UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/1 "2006-11-21T00:08:46Z")

</div>

Ok, so the chapter I am working on now is asking for me to write a  
table of contents with the title left justified and the pages right  
justified. I did this much earlier in the book with individual lines,  
and it came out fine. The code I used initially was :

line\_width = 60  
puts (“Chapter 1: Getting Started”.ljust(line\_width/2) + “page  
1”.rjust(line\_width/2))  
puts (“Chapter 2: Numbers”.ljust(line\_width/2) + “page  
9”.rjust(line\_width/2))  
puts (“Chapter 3: Letters”.ljust(line\_width/2) + “page  
13”.rjust(line\_width/2))

The chapter I am reading now is about arrays, so I am supposed to load  
the same data into an array and accomplish the same task. What Ive  
tried is several variations on this:

chapters = [‘Chapter 1: Getting started’,‘Chapter 2: Numbers’,  
‘Chapter 3: Letters’]  
pages = [‘page 1’,‘page 9’, ‘page 13’]  
line\_width = 60  
puts (chapters.ljust(line\_width/2) + pages.rjust(line\_width/2))

…with the last line having changed a number of times. Obviously, I  
haven’t been able to get the code to run right. Could anyone tell me  
what I am missing?

Shiloh

---

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 21, 2006, 6:05am UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/2 "2006-11-21T06:05:29Z")

</div>

On Nov 20, 2006, at 6:07 PM, Shiloh M. wrote:

> puts (“Chapter 3: Letters”.ljust(line\_width/2) + “page 13”.rjust  
> puts (chapters.ljust(line\_width/2) + pages.rjust(line\_width/2))
> 
> …with the last line having changed a number of times. Obviously, I  
> haven’t been able to get the code to run right. Could anyone tell me  
> what I am missing?\

The problem is that an array doesn’t respond to either ljust or  
rjust. You need to extract the strings from their containing arrays.  
Also, it’s easier to use one array than two, so I recommend something  
like:

source = [  
‘Chapter 1: Getting started’,  
‘page 1’,  
‘Chapter 2: Numbers’,  
‘page 9’,  
‘Chapter 3: Letters’,  
‘page 13’  
]  
line\_width = 60  
until source.empty?  
chapter = source.shift  
page = source.shift  
puts chapter.ljust(line\_width/2) + page.rjust(line\_width/2)  
end

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 21, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/3 "2006-11-21T13:00:46Z")

</div>

Ok, I think i understand that…however the shift function hasn’t been  
introduced yet. Is there another way that i COULD do it?

---

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 22, 2006, 12:53am UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/4 "2006-11-22T00:53:28Z")

</div>

On Nov 21, 2006, at 7:00 AM, Shiloh M. wrote:

> Ok, I think i understand that…however the shift function hasn’t been  
> introduced yet. Is there another way that i COULD do it?

Sure. In Ruby there are always many ways to achieve a goal. How about  
this?

source = [  
‘Chapter 1: Getting started’,  
‘page 1’,  
‘Chapter 2: Numbers’,  
‘page 9’,  
‘Chapter 3: Letters’,  
‘page 13’  
]  
line\_width = 60  
i = 0  
while i \< source.length  
chapter = source[i]  
page = source[i+1]  
puts chapter.ljust(line\_width/2) + page.rjust(line\_width/2)  
i += 2  
end

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 21, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/5 "2006-11-21T13:08:47Z")

</div>

On 11/21/06, Shiloh M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ok, I think i understand that…however the shift function hasn’t been  
> introduced yet. Is there another way that i COULD do it?

If you know the chapters and pages arrays are well-formed (that is,  
they are the same size, and each page corresponds to the chapter in  
the same position) you could do this:

chapters.each\_with\_index {|chapter, index|  
page = pages[index]  
…  
}

martin

---

<div class="post-metadata">

**Author:** ![Shiloh\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@Shiloh\_M](https://www.ruby-forum.com/u/Shiloh_M)\
**Post date:** [November 24, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/using-two-justifications-on-the-same-line/76764/6 "2006-11-24T20:52:41Z")

</div>

Shiloh M. wrote:

/ …

> …with the last line having changed a number of times. Obviously, I  
> haven’t been able to get the code to run right. Could anyone tell me  
> what I am missing?

* * *

#!/usr/bin/ruby -w

chapter\_list = [  
“Getting Started|1”,  
“Numbers|9”,  
“Letters|13”  
]

cn = 1

line\_width = 60

lwd2 = line\_width/2

## chapter\_list.each do |item| chapter\_name,page\_number = item.split("|") puts “Chapter #{cn}: #{chapter\_name}”.ljust(lwd2) + “#{page\_number}”.rjust(lwd2) cn += 1 end

Output:

Chapter 1: Getting Started 1  
Chapter 2: Numbers 9  
Chapter 3: Letters 13
