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 trialDmitriy Kavyazin
15,624 PointsStuck on this question, what am I doing wrong?
Using the has_value? method, check to see if the grocery_item hash has a value called "Bread". If it does, set a new key in the hash called "food" with the value of true.
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread")
hash.store("food", "true")
end
5 Answers
Maciej Czuchnowski
36,441 PointsI'm not sure what hash
means in your example. Also, true
should be a boolean, not a string.
if grocery_item.has_value?("Bread")
grocery_item.store("food", true)
end
Naya Moss
9,894 PointsThanks @Maciej Czuchnowski but, my issue with this is how did you know to use .store? When I tried to use .company, that didn't work.
Maciej Czuchnowski
36,441 PointsNaya, Jason uses this method in one of the previous videos:
http://teamtreehouse.com/library/ruby-collections/ruby-hashes/working-with-hash-keys
around 4:08, as well as in the teacher's notes under that video. There is no built-in method .company in Ruby as far as I know.
Tome Perica
14,604 PointsTask 1
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
grocery_item.has_value?("Bread")
grocery_item.store("food", true)
Task 2
Just add this line of code
grocery_list = grocery_item.values_at("item")
Ivan Kusakovic
12,197 PointsWhat are these hashes? I don't understand it very well, do you have some other interactive tutorials on hashes in Ruby? :) Thanks
Daria Khaylova
5,945 PointsChallenge Task 2 of 2
Use the values_at method on the grocery_item hash to get an array consisting of a single value: the value of the "item" key. Store the returned array in a variable named grocery_list:
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread")
grocery_item.store("food", true)
end
grocery_list = grocery_list.values_at("item")
And it says 'Oops! It looks like Task 1 is no longer passing.' Why?
Rick O'Connell
2,505 PointsYou are using the new variable "grocery_list" in the definition. Change "grocery_list.values_at("item")" to "grocery_item.values_at("item")
Dmitriy Kavyazin
15,624 PointsDmitriy Kavyazin
15,624 PointsOh boy. Noob mistake. Thanks.
Niristotle Okram
2,000 PointsNiristotle Okram
2,000 PointsWhy doesn't it like this grocery_item.store("food", "true")
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 Points"true" is a string, while
true
is a boolean value.