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 trialMarguerite Holden
6,075 PointsI don't see how it follows to set the "food" variable to true .
Can you help me figure this out?
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
hash.has_key?("calories")
true
hash => {"calories", "food"}
3 Answers
jacobproffer
24,604 PointsHey Marguerite,
You can complete this challenge with a simple if statement. If the hash has the key "calories", declare a new variable called food and assign it the value of true:
if hash.has_key?("calories")
food = true
end
This can also be simplified:
hash.has_key?("calories")
food = true
Jason Anello
Courses Plus Student 94,610 PointsThere might be a problem with this challenge.
Your simplification is going to set food
to true regardless of whether the key exists or not because it's not part of an if statement.
You could do
food = true if hash.has_key?("calories")
jacobproffer
24,604 PointsHey Jason,
I just checked this out. That is interesting. Is there a way around it without an if?
Jason Anello
Courses Plus Student 94,610 PointsHey Jacob,
I don't know if there is anything that would match the instructions. Your first code example is pretty much what the instructions are guiding you to do.
We're not told what food
should be if it doesn't have the key. It might be false
or nil
Here's some other things you might be able to do, instead of an if, depending on what you want food to be if the key wasn't found.
if food
should be set to true or false you could do
food = hash.has_key?("calories")
if food
should be set to true or nil
# you could use the ternary operator but it's really a shortened version of an if/else
food = hash.has_key?("calories") ? true : nil
# or you could use the logical or operator
food = hash.has_key?("calories") || nil
These are probably not as straightforward as using an if
Is that what you were asking about or did I misunderstand?
Marguerite Holden
6,075 PointsI like both answers and was hard picking the best answer. Thx so much for your help.
Jay McGavren
Treehouse TeacherHello Marguerite Holden and Jacob Proffer ,
Jason Anello is correct. This code should not have been able to pass the challenge:
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
hash.has_key?('calories')
food = true
...But it did, because previously Treehouse only tested the code submission with a hash that did have a "calories"
key. We did not test the submission with a hash that did not have a "calories"
key to ensure the code still worked correctly.
I have fixed the tests for the challenge, which means the above code will no longer pass. The correct answers indicated elsewhere in this thread will still work, however.
jacobproffer
24,604 PointsHey Jay,
Thanks for the update.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsTagging Jay McGavren
There might be a problem with the tester on this one.
It only seems to be checking that the has_key? method has been called and that the
food
variable exists, not necessarily a true value.