Ruby Ruby Collections Ruby Hashes Working with Hash Values

Matthew Barona
Matthew Barona
1,704 Points

Why is this Ruby block wrong...?

How is it that I'm getting an error stating the has_value? method has not been used...?

if grocery_item.has_value?("Bread") == true grocery_item = {"food" => true} else return false end

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

if grocery_item.has_value?("Bread") == true
    grocery_item = {"food" => true}
else
  return false
end
Troy Gooden
Troy Gooden
4,340 Points
if grocery_item.has_value?("Bread")
  grocery_item["food"] = true
end

This is what i wrote to produce what you needed in your code. If you look i passed the "has_value" an argument of "Bread" in my first line....I didn't try to do a comparison operator. What happens is if the line that i put is right, it will return true anyway. The line after which is how i add to my hash. You do not need to reassign the whole grocery hash by doing "grocery_item =" .... what you need to do is add on to the pre existing hash already. There are multiple ways of doing this. Hopefully this helped.