Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

why do assign a blank var after a var has come back with an input?

im on a lesson creating a simple ruby program.

Im confused why when we call the ask method in the add_contact method that we have to assign a blank string to the answer var when an input has been returned to that var?

I have highlighted the line in question with an arrow. thanks for any help!

def ask(question, kind="string") print question + " "
answer = gets.chomp answer = answer.to_i if kind == "number" return answer end

def add_contact contact = {"name" => "", "phone_numbers" => []} contact["name"] = ask("what is the persons name?") answer = "" <-------------------------------------------------------------------------- while answer != "n" answer = ask("do you want to add a phone number? (y/n)") if answer == "y" phone = ask("Enter a phone number: ") contact["phone_numbers"].push(phone) end end return contact end

contact_list = []

answer = "" while answer != "n" contact_list.push(add_contact()) answer = ask("Add another? (y/n)") end

1 Answer

Brandon McClelland
Brandon McClelland
4,645 Points

You're setting it to "" to explicitly initialize the variable to something other than 'n' before starting the while statement.