Ruby Ruby Collections Ruby Hashes Working with Hash Values

Dana Weiss
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Dana Weiss
PHP Development Techdegree Graduate 22,293 Points

Ruby Hashes: It keeps looking for a key I'm not asking it to look for!

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" } grocery_list = grocery_item.values_at("item")

Result: Bummer! The key food was not found in the grocery_item hash.

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
grocery_list = grocery_item.values_at("item")

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,838 Points

You should use has_value? method on the grocery_item Hash to check if it contains value "Bread" , and you should write a conditional clause to do that.

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

if grocery_item.has_value?("Bread")
  # set a new key in the hash called "food" with the value of true.
end

Replace the comment section w/ your own code to pass the challenge.

William Li
William Li
Courses Plus Student 26,838 Points

Generally speaking, for a multi-parts code challenge, do NOT modify/delete the code written during the previous parts unless the current challenge description asks you to do so explicitly. The reason for that is because every time you click the Check Work button, The test cases always checks for the correctness of all the code you've written so far.