James Gray
7/27/2006 9:48:00 PM
On Jul 27, 2006, at 11:44 AM, Nate Turnage wrote:
> On 7/27/06, James Edward Gray II <james@grayproductions.net> 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.
I see you are already getting some hints, but here's another. You
can nest Arrays in Ruby. Your book did show this, but only on one
line and it wasn't explained much. Have a look though:
>> [ ["I", "First Chapter", "Page 20"],
?> ["II", "Second Chapter", "Page 43"],
?> ["III", "...", "Page 102"] ].each do |chapter|
?> puts chapter[0]
>> puts chapter[1]
>> puts chapter[2]
>> end
I
First Chapter
Page 20
II
Second Chapter
Page 43
III
...
Page 102
=> [["I", "First Chapter", "Page 20"], ["II", "Second Chapter", "Page
43"], ["III", "...", "Page 102"]]
See if that gives you some new ideas.
James Edward Gray II