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 trialRoger Nordqvist
7,007 PointsDont 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_list = []
contact = {"name" => "", "phone_number" => "" }
def get_name
print "Enter name: "
gets.chomp
end
contact["name"] = get_name
5 Answers
Salman Akram
Courses Plus Student 40,065 PointsHi 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
7,007 PointsSorry 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 :)
Salman Akram
Courses Plus Student 40,065 PointsAwesome! 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
7,007 PointsIf 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.
Salman Akram
Courses Plus Student 40,065 PointsWhat 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
7,007 Pointsalso tried: contact_list.push(contact)
This also tells me Task 1 now fails.
Salman Akram
Courses Plus Student 40,065 PointsSee above that solution, we don't need to add extra codes, that's why it will get error messages.
Roger Nordqvist
7,007 PointsOK ... thank you. It did not feel obvious what was needed. Thanks for your patience, Salman. Much appreciated.
Salman Akram
Courses Plus Student 40,065 PointsNo problem Roger, we can keep trying and will become more familiar with Ruby loops.
Roger Nordqvist
7,007 PointsRoger Nordqvist
7,007 PointsThank you for the quick clearification.
:)