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

kevinthecoder
kevinthecoder
10,791 Points

Unable to get past Has_value? Challenge

Alright, this one has me stumped. I have re-played two different videos a total of three times plus carefully looked at my code for syntax errors, missing quotes, etc. I even tried single and double = signs (it should be ==). What am I doing wrong? My if statement should be good. Help!

hash.rb
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if grocery_item.has_value?("Bread") == true
  grocery_item = {"food" => true}
end

4 Answers

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

Hi, kevinthecoder

# there's a problem with this line
grocery_item = {"food" => true}
# instead of just setting the value for "food" key
# you're essentially assigning grocery_item to a completely new Hash


# change it to
grocery_item ["food"] = true
Luke Buśk
Luke Buśk
21,598 Points

This is how it should look:

      if grocery_item.has_value?("Bread")
      grocery_item["food"] = true
      end

You do not need to specify "== true" because "has_value?" already returns true if such item is present.

kevinthecoder
kevinthecoder
10,791 Points

Guys, this still did NOT work. I tried both with brackets and parenthesis and it still doesn't work. I'll try to paste below (I'm trying to stick to the same forum post and not a new forum post):

if grocery_item.has_value?("Bread") = true

grocery_item["food"] = true

end

Luke Buśk
Luke Buśk
21,598 Points

Remove the " = true" in the first line. Remember that one equal sign assigns a variable, two equal signs (==) checks equality. And as i mentioned earlier You do not need to specify it, the has_value? already returns true if it finds "Bread".

kevinthecoder
kevinthecoder
10,791 Points

Ah, OK....I think I got it now! I passed this Challenge and the next one....thanks for the help! I'm on to the last new section (grocery list).