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 trialMayank Munjal
Front End Web Development Techdegree Graduate 18,120 PointsNot sure what's wrong with my code!
Can't figure out the error, any help will be appreciated!
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("bread")
grocery_item["food"] = true
end
Brian Gilbank
9,586 PointsYou need to replace grocery_item["food"] = true with
grocery_item.store("food", true)
Mayank Munjal
Front End Web Development Techdegree Graduate 18,120 PointsAlright but isn't this an alternative way to add a key and a value to a hash as well?
1 Answer
Sorin Pantis
5,138 PointsMayank Munjal what you're doing is correct, and you shouldn't get any errors. The only thing to notice is that Ruby is case sensitive, so "bread" is not the same as "Bread", thus in your case grocery_item.has_value?("bread")
will return false.
If you do this, it will set your food
key to true
:
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread") # notice I'm checking the exact case as in the hash above
grocery_item["food"] = true
end
puts grocery_item["food"]
Let me know if that works out for you! ?
Mayank Munjal
Front End Web Development Techdegree Graduate 18,120 PointsMayank Munjal
Front End Web Development Techdegree Graduate 18,120 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 in the hash called "food" with the value of true.