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,708 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