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 trialRichard Poirier
Courses Plus Student 2,930 PointsHow do you set variables to be true if something is present in a hash
I have been working on this challenge for some time and I can't figure it out...I totally get how to search a hash for a key, but how do you write the code following it to make a new variable called food = true if the key is present? The attached code as just my last ditch effort to figure it out....
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
hash.has_key?("calories")
if hash.fetch("calories")
"Food"=true
end
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Richard,
You're close but you want the result of the has_key? method to be the condition for your if
statement.
if hash.has_key?("calories")
# set a variable named food to true
end
Also, be careful with the assignment. It's a variable named food
and not a string containing Food