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 trial

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

1 Answer

Troy Gooden
Troy Gooden
4,352 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.