Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that we're familiar with loops and iteration, let's practice our knowledge by writing a simple contact list program.
Code Samples
Here is the code we write in this portion of the video.
First, we set up a contact list array:
contact_list = []
Next, we define the ask
method. We're going to be asking the user for input quite a bit so the ask
method will repeat the logic for us.
def ask(question, kind="string")
print question + " "
answer = gets.chomp
answer = answer.to_i if kind == "number"
return answer
end
The ask
method takes two arguments and defaults to having the kind
of answer be a string. This will let us return numeric values if we want.
Finally, we test the ask
method:
answer = ask("What is your name?")
puts answer
You need to sign up for Treehouse in order to download course files.
Sign up