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

Michael Cook
Michael Cook
5,864 Points

Hi! I'm not exactly sure what to do in Ruby Loops section 4, where I have to assign hash values from methods. Thanks!

Just as the title says! I tried some code here and there, but with this one, it just really hits me hard. I don't have much clue on what to do. The code below is the starting code the challenge gives you.

contact.rb
contact_list = []

contact = {"name" => "", "phone_number" => "" }

5 Answers

Antonio De Rose
Antonio De Rose
20,885 Points

question says, that assume that the function for get_name is already there with no parameters, and asks you to call the get_name function, and the return value, should update the hash name in the contact array

contact_list = []

contact = {"name" => "", "phone_number" => "" }

#I have written the function for you, which you do not have to write, 
#just to give you an understanding on what the broader level of question
#in the challenge you should not write this below function, instead how you
#call that function to update the name hash in contact array
def get_name()
  print "what is your name ? "
  return gets.chomp
end
Michael Cook
Michael Cook
5,864 Points

Thanks, that helped a little, but I'm not sure how to exactly have get_name edit the hash at all.

Michael Cook
Michael Cook
5,864 Points

Looking at the videos and watching them again, I'm not sure how to get get_name to edit the hash without editing get_name itself, sorry if I'm missing something important.

Michael Cook
Michael Cook
5,864 Points

Alright, well I figured it out on my own, but thanks for your help! I appreciate it!

Dude...

Others have befallen your turmoil.

What was the fix?

Ok I too figured it out...

Don't listen to Antonio De Rose's answer as it implies you must pass a string to the get_name method.

The answer is you're overthinking it... Just call the method IN the hash.