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 trialjulianordhem
4,060 PointsUsing the has_value? method, check to see if the grocery_item hash has a value called "Bread". If it does, set a new key
I don't know what I'm doing wrong help!
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company"}
if
grocery_item.has_value?("Bread")
grocery_item = {"food" => "true"}
end
2 Answers
Ari Misha
19,323 PointsHiya there! The challenge seems simple enough and straightforward. In the task 1, you need to set food key to true, if grocery_item hash contains value "Bread". And in the task 2, you need to use values_at method in Ruby to get the value at the key item in the grocery_item hash, and store the result in grocery_list variable. Here is how your code should look like:
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
grocery_item["food"] = true if grocery_item.has_value? "Bread"
grocery_list = grocery_item.values_at("item")
~ Ari
Samuel Ferree
31,722 Pointsyou're checking the hash correctly, but you're adding a new key/value pair incorrectly.
when you're adding a new key/value pair to a ruby hash that is already declared, use the syntax hash[key] = value;
see below:
if grocery_item.has_value?("Bread")
grocery_item["food"] = "true"
end
julianordhem
4,060 Pointsthanks so much!!
julianordhem
4,060 Pointsjulianordhem
4,060 PointsThank you!! :)
alan santos
4,125 Pointsalan santos
4,125 PointsThank you very much Ari! It was really helpful.