On 21 Aug 2001 09:15:54 +0900, Joseph McDonald wrote:
> File.open('phonespec.txt', 'a') do |myfile|
>
>   puts "Welcome to MyAddressbook. Please follow the prompts."
>   puts "If you wish to end data entry, you may do so at any time"
>   puts "by type the word END into a prompt."
> 
>   catch (:done) do
>     loop do
>       result = []
>       ["First name", "Last name", "Phone"].each do |prompt|
>         print "\t#{prompt}: "
>         result << gets.chop
>         throw :done if result[-1] == "END"
>       end
>       myfile.puts result.join("\t")
>     end
>   end
> end

Next round... :)

puts <<EOF
Welcome to MyAddressbook. Please follow the prompts.
If you wish to end data entry, you may do so at any time
by type the word END into a prompt.
EOF

File.open('phonespec.txt', 'a') do |myfile|
  catch (:done) do
    questions = ["First name", "Last name", "Phone"]
    loop do
      answers = questions.map do |prompt|
        print prompt + ": "
        (str = gets.chomp) != "END" ? str : throw(:done)
      end
      myfile.puts answers.join("\t")
    end
  end
end

puts "To start the program again please type intro2.rb"

The use of the "answers" variable can be eliminated by call chaining,
but I like the variable telling me what's happening.

This is the first time I've used catch and throw - these little
exercises are useful. :)