Using two justifications on the same line.

S

Shiloh Madsen

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
 
W

William James

Shiloh said:
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

chapters = ['Chapter 1: Getting started', 'Chapter 2: Numbers',
'Chapter 3: Letters']
pages = 'page 1', 'page 9', 'page 13'
line_width = 60
puts chapters.zip( pages ).map{|chapter,page|
chapter.ljust(line_width/2) + page.rjust(line_width/2) }
 
M

Morton Goldberg

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?\

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
 
S

Shiloh Madsen

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


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?\

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
 
M

Martin DeMello

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
 
M

Morton Goldberg

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
page = source[i+1]
puts chapter.ljust(line_width/2) + page.rjust(line_width/2)
i += 2
end

Regards, Morton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top