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

jakefish
jakefish
7,961 Points

I'm struggling with the second part of these questions--setting the value or variable to be true...

How do I set the value or variable to be true?

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

1 Answer

Ben Shockley
Ben Shockley
6,094 Points

So what this challenge is wanting you to do is check to see if a value exist in the hash, and if it does, create a new key with the value of true.

So this is the code that worked for me.

First you want to use an if statement to check to see if the value exist in the has. Then if the value is found, you want to create a new key in the hash with the value of true. If the value is not found, you don't need to do anything.

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

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