Helpful Information
 
 
Category: Ruby Programming
Repeating previous lines of code?

hi, i'm new to ruby. The tutorials i am learning from have a assignment to make an address book. i have wrote a poor but working script that holds address info in the script itself.

So far the program the program lists the available contacts
,states how many contacts you have, asks which contact you want to view and shows their details . It then asks if you would like to view another.

And that's where i am stuck i can't get the get the program to start over. Could someone point me in the right directions

cheers

without seeing your code structure, this is - at best - a shot in the dark.
I can only assume that you are doing what you are asked like so:
# pseudocode
build_tables
ask_for_input
show_appropriate_entryHowever, you want to repeat the last two items for as long as the user is interested. In order to do that, simply put those two statements in a loop.
#pseudocode
build_tables
while user_wants_to_continue
ask_for_input
show_appropriate_entry
end

If that is not the case, please provide more information so that further answers will be more helpful.

Sorry i should have added the code on the first post

#CONTACT LIST#


Contacts = [ 'Stephen ', 'Chris ', 'Catherine ', 'Michelle ', ] .sort


puts '---AVAILABLE CONTACTS---'
puts 'You have ' + Contacts .length .to_s + ' contacts

'

Contacts .each do |contact|

puts contact

end



puts " "
puts 'Which contacts address would you like?'

STDOUT .flush

Contact = gets .chomp


puts '

'
STDOUT .flush

#CONTACT INFORMATION#

case Contact
when "stephen ", "steve"
puts "hello steve"
puts 'do you not know your own address'
when 'Chris ' , 'chris'
puts 'street and house numer'
puts 'vilage'
puts 'town'
puts 'county'
puts 'postcode'
puts ' phone number'
when "Catherine ", 'katy'
puts 'street and house numer'
puts 'vilage'
puts 'town'
puts 'county'
puts 'postcode'
puts ' phone number'
when "Michelle ", 'shell'
puts 'street and house numer'
puts 'vilage'
puts 'town'
puts 'county'
puts 'postcode'
puts ' phone number'
else
puts "There are no contacts with this name"
end


puts " "
puts 'would you like any other address? '
STDOUT .flush

again = gets .chomp

For future reference, please use code tags (they help the formatting of the text).

When you do the first gets.chomp, you want to have your loop control directly before that (unless you want to print the list each time).
That can happen several ways, what follows is just one...


again = 'yes'
while again =~ /[Yy](es)?/

#process your code as you have it

endOr you can decide for a little cleaner approach, and combine the check with the data gathering
while gets.chomp =~ /[Yy](es)?/

# process your code here

end

That should get you started. Post back with any more problems you encounter.










privacy (GDPR)