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 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,429 Points

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

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


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

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