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 Keys

Is the has_key? challenge asking for an "if" statement?

So far this course has been straight forward and very simple, but I'm confused at what this particular statements is asking of me.

Using the has_key? method, check if the hash variable has a key called "calories". If it does, set a new variable called "food" to true.

Is it asking for an "if" statement? How would I solve this one?

Joshua Shroy I don't know if you're still on TH, but I'm very frustrated with the challenge here because the video beforehand's content doesn't match the challenge at all. We didn't go over if statements in hashes....like at all yet. This doesn't seem fair or reflective of the content in the video preceding this challenge. So I think you're totally right to be confused. I am too.

Jason Seifer : Can you please update the challenge here to be more appropriate to the preceding video's content?

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You are on the right track, but this is enough:

food = true if hash.has_key?("calories")

or

if hash.has_key?("calories")
  food = true
end

Remember that no matter what the exercise says, names of the variables are always without quotes. They also didn't ask you for any method definitions, so def will not pass this exercise.

Thank you Maciej. Looks like I had the food variable in quotes. Won't make that mistake again ;)

Ian Crawford
Ian Crawford
22,312 Points

You are correct in assuming it is asking for an if statement.

Inside the if statement it wants you to make food equal true if the statement is true.

I tried implementing some forms of "if" statements but nothing is working so far. I'm not sure what I should be "defining" either.

def something
     if hash.has_key?("calories")
        "food" = true
    end
end