Ruby Ruby Collections Ruby Hashes Working with Hash Values

Dana Weiss
Dana Weiss
PHP Development Techdegree Graduate 22,243 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
PRO
William Li
Pro Student 26,794 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.

Dana Weiss
Dana Weiss
PHP Development Techdegree Graduate 22,243 Points

ah I see. After I completed that step the directions no longer included that so I deleted it! Thanks!

William Li
William Li
Pro Student 26,794 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.