From: William James Date: 2006-07-28T04:40:11+09:00 Subject: Re: Learn to Program - Solutions? Nate Turnage wrote: > On 7/27/06, James Edward Gray II wrote: > Refresh my memory on what that exercise is and I'm happy to give a > > hint or two. > > "Rewrite your table of contents program on page 35. Start the > program with an array holding all of the information for your table > of contents (chapter names, page numbers, etc.). Then print out > the information from the array in a beautifully formatted table of > contents." > > The chapter is about array methods. For each chapter there is a > chapter number, a chapter title, and a page number. I understand how > the info for each chapter > can live in an array, but I don't understand how all of the data for > all of the chapters lives in a single array that holds all that data. > Thanks in advance. > > pixelNate toc = ['Table of Contents', 'Chapter 1', 'Getting Started', 'page 1', 'Chapter 2', 'Numbers', 'page 9', 'Chapter 3', 'Letter', 'page 13'] wd = 60 puts toc.shift.center(wd) toc.inject([]){ |a,e| a << e if 3 == a.size a[0,2] = a[0,2].join( ": " ) + " " a.push " " + a.pop[ /\d+/ ] print a.first, "." * (wd - a.first.size - a.last.size), a.last, "\n" a.clear end a }