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 Ruby Loops Build a Simple Contact List Assigning Hash Values From Methods

What does this problem want exactly ?

"Assign the value of the key name to the return value of the method get_name() in the contact hash. Assume that get_name() returns a string. "

I even tried this way;

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

def add_contact contact = {"name" => "", "phone_number" => "" } contact["name"] = get_name("enter a name please") return contact end

contact_list.push(add_contact())

On my terminal everything seems ok, I can see output of the hash with inspect method but here I couldnt solve it

Thank you

contact.rb
contact_list = []
contact = {"name" => "", "phone_number" => "" }

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

contact["name"] = get_name("enter a name please")

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

The instruction is poorly worded. You need to set the value of the Contact hash at "name" key to the return value of the get_name() method. You don't need to write the method, just assume that is already written:

contact_list = []

contact = {"name" => "", "phone_number" => "" }
contact["name"] = get_name()

You need to do something similar for the next task.

Thank you very much sir