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

Roger Nordqvist
Roger Nordqvist
7,007 Points

Dont understand the question, I think

I dont understand the objective of this code challange. Could someone clearify?!

This is the objective: 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.

The error I'm getting: Errno::ENOENT: No such file or directory @ rb_sysopen - 1

contact.rb
contact_list = []

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

def get_name
  print "Enter name: "
  gets.chomp
end

contact["name"] = get_name

5 Answers

Hi Roger,

You just need to put VALUE of the method "get_name()" into KEY "name" inside contact hash.

Key-Values Pairs : Hash = { KEY, VALUES }

employer = { "Matt" => 100, "Jerry" => 600 }

You could write like this below:

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

More information - http://ruby-doc.org/core-2.2.0/Hash.html

Hope that helps.

Roger Nordqvist
Roger Nordqvist
7,007 Points

Thank you for the quick clearification.

:)

Roger Nordqvist
Roger Nordqvist
7,007 Points

Sorry for bothering you all, again :)

In the next assignment Im asked to append the has to the array:

contact_list = []
contact = {"name" => get_name, "phone_number" => get_phone_number }

def get_name
  print "Enter name: "
  gets.chomp
end

def get_phone_number
  print "Enter phone number: "
  gets.chomp
end

contact_list << contact

puts contact_list.inspect

In rubymine I get:

Enter name: roger

Enter phone number: 1234

[{"name"=>"roger", "phone_number"=>"1234"}]

This works in rubymine, but not in the assignment.

I've had no problems so far with the assignments but it feels like I've just hit a brick wall :)

Awesome! The append means we have to push contact hash into contact_list array. The method is push to append it.

Something like this - contact_list.push(contact) , remove definition and method of both get_name and get_phone_number, the challenge asked you only to follow the question. ;)

Example: @3:41

Roger Nordqvist
Roger Nordqvist
7,007 Points

If im reading your answer correctly you are telling me to:

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

contact_list.append(contact)

Then it tells me that Task 1 fails. Even if I dont remove the definition and method for both functions, it tells me that task one now is failing.

What I mean is the challenge doesn't want you to add extra codes like below except only task 1, 2, and 3 we did.

def get_name
  print "Enter name: "
  gets.chomp
end

def get_phone_number
  print "Enter phone number: "
  gets.chomp
end

Also assign its return value to the hash's name key:

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

contact["name"]=get_name()
contact["phone_number"]=get_phone_number()
contact_list.push(contact)
Roger Nordqvist
Roger Nordqvist
7,007 Points

also tried: contact_list.push(contact)

This also tells me Task 1 now fails.

See above that solution, we don't need to add extra codes, that's why it will get error messages.

Roger Nordqvist
Roger Nordqvist
7,007 Points

OK ... thank you. It did not feel obvious what was needed. Thanks for your patience, Salman. Much appreciated.

No problem Roger, we can keep trying and will become more familiar with Ruby loops. :wink: