



(6 ratings)
Printing complex structures
You can still type "puts addressbook", but the output is ugly and not very useful (try it and see for yourself). We would rather define our own way of printing its contents.
The addressbook is an array, so we have the Array#each method. Let's start by just printing the first names of the contacts:
addressbook.each do |person| |
This will print:
Melissa |
Full names
The next step is to print full names:
addressbook.each do |person| |
Which prints:
Melissa Adams |
Phone number:
addressbook.each do |person| |
Output:
Melissa Adams: |
Address
Finally add the address and a sepparation between the entries.
addressbook.each do |person| |
Which produces:
Melissa Adams:
(301) 364-8924
23 St George St.
Silver Spring
MD, 20465
Joe Smith:
(301) 345-9837
43 Main St. W
Washington
DC, 29847
Sandy Koh:
(301) 354-2975
324 Campus Dr.
College Park
MD, 23659
20 Random Tutorials from the same category :
Example: Addressbook RUBY
Classes and methods in RUBY
Sorting the addressbook RUBY
What can ruby arrays do?
Arrays In RUBY
The Scalability of Ruby
Iterators In RUBY
Hashes In RUBY
Writing iterators In RUBY
Progress Bars with GD2 and Ruby
Printing the addressbook RUBY
Difference of Gtk+ and Ruby/Gtk
Implementing AddressBook In RUBY
More features IN RUBY













