Missing parantheses maybe?

Trying to do some fancy formatting and getting an error message.
Since I know .center is a method the error message can’t be the
ultimate truth. Anyone ?

                      Table of Contents    <------This is corect

toc.rb:8: undefined method `center’ for [2]:Array (NoMethodError)

Exit code: 1

toc = [‘Table of Contents’, ‘Chapter 1’, ‘Getting Started’,
‘page 1’, ‘Chapter 2’, ‘Numbers’, ‘page 9’,
‘Chapter 3’, ‘Letter’, ‘page 13’]

    line_width = 70

    puts(              toc[0].center(line_width))
    puts(              toc[1].ljust(line_width/2) +

[2].center(line_width) +[3].rjust(line_width/2))
puts( toc[4][5][6])
puts( toc[7][8][9])

Tia
Stuart

2006/6/24, Dark A. [email protected]:

Trying to do some fancy formatting and getting an error message.
Since I know .center is a method the error message can’t be the
ultimate truth. Anyone ?

I’m not sure what you’re implying here, but as the error message
indicates Array#center is nonexistent. You probably want
String#center as in

irb(main):002:0> “fff”.center 10
=> " fff "

You also probably missed a toc there:

    puts(              toc[0].center(line_width))
    puts(              toc[1].ljust(line_width/2) +

[2].center(line_width) +[3].rjust(line_width/2))
^^^

“toc” missing here.

    puts(              toc[4][5][6])
    puts(              toc[7][8][9])

Cheers

robert

Hopefully it was a beginners mistake and not a pattern of my own logic
:slight_smile:
Thanks Robert!

Stuart

.rjust dosn’t seem to be working correctly, no error, but it’s not
right justifying either.

my output for the .rjust looks like this:
Chapter 1 Getting Started
page 1
Chapter 2 Numbers
page 9
Chapter 3 Letter
page 13

here is the first line (Chapter 1…)

puts( toc[1].ljust(line_width) + toc[2].ljust(line_width)

  • toc[3].rjust(line_width))

I hand tweaked teh .rjust parameter with hard numbers .rjust(20)

    .rjust(25), etc and that helped but I thought .rjust would

make sure the last letters of the strings would all line up.

Stuart

2006/6/24, Dark A. [email protected]:

here is the first line (Chapter 1…)

puts( toc[1].ljust(line_width) + toc[2].ljust(line_width)

  • toc[3].rjust(line_width))

I hand tweaked teh .rjust parameter with hard numbers .rjust(20)

    .rjust(25), etc and that helped but I thought .rjust would

make sure the last letters of the strings would all line up.

For this scenario sprintf / printf are probably better suited.

Kind regards

robert