#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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:
    Code:
    # pseudocode
    build_tables
    ask_for_input
    show_appropriate_entry
    However, 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.
    Code:
    #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.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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...

    Code:
    again = 'yes'
    while again =~ /[Yy](es)?/
    
       #process your code as you have it
    
    end
    Or you can decide for a little cleaner approach, and combine the check with the data gathering
    Code:
    while gets.chomp =~ /[Yy](es)?/
       
       # process your code here
    
    end
    That should get you started. Post back with any more problems you encounter.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo