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

has_value? method not working

The has_value? method does not work in this script-er.

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

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

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

The has_value? method is actually working just fine here, and you're calling and using it correctly. It's what you're doing inside the if block that isn't right here. The challenge asks you to add a new key "food" inside that if block, but your current implementation overwrites the entire hash with a new one (which is what the assignment operator (=) does to variables). You can add a new key to a hash by assigning to subscript syntax, however, which looks like this:

some_hash = {key: 1}

some_hash[:new_key] = 2 # This is the line I'm talking about

# some_hash is now {key: 1, new_key: 2}

Once you change the line in your if block to use the subscript syntax instead of raw assignment, it passes the challenge just fine

Thank you.