More help with address book

I’m trying to create a simple address book in ruby. It is made up from
the following classes. Address, Phone, Person, and AddressBook . The
Address, Phone, Person classes all work fine. I’m having problems with
several methods in my AddressBook class.

Add(person) needs to check if that person already exists. I tried using
my query method but it only prints the name to the screen. Can I make
it also return an person object to use as a comparison?

The prompt method should take a command (add, print, remove, find,
exit) then depending on which command ask the user for required
information. The command prompts should run continuously until “exit”
is entered. The add command which is the most complicated of the five
works fine for a single time. I can’t figure out how to implement a
while condition to keep it going without getting errors.

I’m also having problems with the remove method logic. I built the
remove method to take a variable name as a parameter, but the command
prompt would require an first and last name instance fields from the
person class.

I'm getting close just a few tweaks here and there.
This code is getting long one so I just attached the file. If you think
I should copy and paste let me know.

Attachments:
http://www.ruby-forum.com/attachment/5368/Ruby_AB.rb

···

--
Posted via http://www.ruby-forum.com/.

I’m trying to create a simple address book in ruby. It is made up from
the following classes. Address, Phone, Person, and AddressBook . The
Address, Phone, Person classes all work fine. I’m having problems with
several methods in my AddressBook class.

Add(person) needs to check if that person already exists. I tried using
my query method but it only prints the name to the screen. Can I make
it also return an person object to use as a comparison?

You should remove the printing from the finding because this is a completely different aspect. You basically have two options to improve this:

1. Change #query to return an Array of found items.

2. Change #query so that it yields every matching entry to a block passed to the method.

Advantage of both approaches is that you can reuse the query code in different situations. I would go with version 2 but 1 might be easier to understand at first. If you do 2 you could code like this

def print_query(name)
   query name do |person|
     puts person
   end
end

def query_first(name)
   query name do |person|
     return person
   end

   nil
end

def add(name, ...)
   raise "Person known already" if query_first(name)
   ...
end

The prompt method should take a command (add, print, remove, find,
exit) then depending on which command ask the user for required
information. The command prompts should run continuously until “exit”
is entered. The add command which is the most complicated of the five
works fine for a single time. I can’t figure out how to implement a
while condition to keep it going without getting errors.

I would create a class per command. This class can contain the prompt text as well as the evaluation logic. You could then stuff instances of those classes in a Hash and just look them up via command.

But: I would remove the code from AddressBook. Currently you are mixing model code (i.e. data management of address books) with user interface (output, prompting etc.). That's usually considered bad because you mix logic with presentation.

I’m also having problems with the remove method logic. I built the
remove method to take a variable name as a parameter, but the command
prompt would require an first and last name instance fields from the
person class.

I'm getting close just a few tweaks here and there.
This code is getting long one so I just attached the file. If you think
I should copy and paste let me know.

You could host this on github. That way you also have version control and can look at the path of evolution later.

Kind regards

  robert

···

On 14.11.2010 13:17, Aaron Haas wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

The prompt method should take a command (add, print, remove, find,
exit) then depending on which command ask the user for required
information. The command prompts should run continuously until “exit”
is entered. The add command which is the most complicated of the five
works fine for a single time. I can’t figure out how to implement a
while condition to keep it going without getting errors.

About this part I think input "command" and its params once will be more
flexible.
add [Aaron Haas]{1144 S. Fourth, Columbus, Ohio, 43209}
more acceptable than:
[
add
Enter first name:
Aaron
...blah
...blah
]

u can use regexp to make friendly command line
<command> [<name info>] {<addr info>}
\[(.*)\]\s*\{(.*)\}\s*(\+\d{,10}\+)?

As "Robert" said,seperate detail processing code from user interface is
good for extension and maintanace.

Also from UE(user experience) provide more foolish inputting way,just
pick up useful information in clutter,it's painful for me to adjust your
code when I use your defined class like Address,Person...That's too much
fields need to be filled in.Any way if you provide GUI it's another
thing,but now in CLI,the simple is the best.

I made a Command class put all cmd processing code in it.Defining a
class method to provide a command list.Left "prompt" as a command
dispatcher.
U can take a look :slight_smile: Hope that can help u.

Attachments:
http://www.ruby-forum.com/attachment/5383/problem101114.rb

···

--
Posted via http://www.ruby-forum.com/\.