



(1 ratings)
Our strategy will be to split the problem into simpler ones.
First step: requirements
First, we should figure out what kind of information our address book should have:
The address book contains a group of contacts . We should be able to sort these contacts alphabetically.
Each contact has a first name, last name, phone number and an address.
Each address contains a street, city, state and zip code.
We are going to tackle these starting and the address structure and finishing the the addressbook.
Second step: address
We have a couple of choices for the address structure:
Array: It is customary that the street goes first, then city, then state and last the zip code. So an array might work.
Hash: It is easier to remember something like address["zip"] than address[2]. So, a hash might be easier to use.
In this case, I will pick a hash. So, the three addresses would be:
# Melissa's address |
Third step: Persons
Each person has a first name, last name, a phone number and an address. There is no obvious order for these items, so we definitelly need a hash.
# Joe |
Fourth step: Addressbook
Now that we have all the other structures defined, it is time to create the addressbook. We want the addressbook to retain a particular order. Thus, we must use an array:
addressbook = [ melissa, joe, sandy ] |
And there we are. A complete addressbook structure. In the next section we will learn how to sort this addressbook and do some other nifty things with it.
| Note: Make sure that you save all of this in a file. We are going to keep using it in the next few sections. |
Excercises
Add one of your own friends to this addressbook.
Change the addressbook so that it also contains an email address.
20 Random Tutorials from the same category :
Example: Addressbook RUBY
Implementing AddressBook In RUBY
Sorting the addressbook RUBY
Iterators In RUBY
Printing the addressbook RUBY
The Scalability of Ruby
Writing iterators In RUBY
What can ruby arrays do?
Progress Bars with GD2 and Ruby
More features IN RUBY
Arrays In RUBY
Classes and methods in RUBY
Difference of Gtk+ and Ruby/Gtk
Hashes In RUBY













