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

Don't understand why I continue to receive an error message on this code challenge?

Hello anyone,

I have been working through this particular challenge for about 2 days now and can't seem to figure out why I keep receiving the same error message, despite having tried multiple ways of writing the code. I know that I may be making a syntactical error here somewhere but I'm really struggling to figure out how. I've even written the code in irb on workspaces to make sure that my logic is sound and I've completed the objective if workspaces is to be believed. The error message I keep receiving is as follows: "We tried a different hash without your "calories" key and your "food" variable was still set to true. Check your code!"

I'm really new to this and I do not know what that means. Any help would be greatly appreciated.

Thank you, -Javan-

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
food = {"name" => "Bread", "quantity" => 1, "calories" => 100 }
food == hash

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, so the first line you wrote - hash.has_key?("calories") - is a great start. It checks if the hash has this key. This expression will either return true or false. If we have a task that asks us to do something conditionally (i.e. depending on whether something is true or false), we should use an if statement. We can use them in a single line or in multiple lines, depending on your coding style. We assign variables using the single = sign. Knowing all this, we need to construct an if statement that - if an expression returns true - assigns the value of true to a variable named food (the use of quotes around that name is unfortunate). Some sample code snippets that could inspire you:

if 'a' == 'a' # expression that returns true or false, true in this case
  variable = true # an assignment of a value to a variable

If you prefer one line, it could look like this:

variable = true if 'a' == 'a'

and if you want to be fancy, you can make this even shorter:

variable = 'a' == 'a' # this basically checks whether 'a' is equal to 'a' and then it assigns that boolean value to the variable

These should give you an idea of how to proceed next! Let me know if you have any more problems.

Ari Misha
Ari Misha
19,323 Points

Hiya there! I feel ya , Javan, even though it took me literally 5 seconds to figure it out lol. Anyways the thing you're not doing what the challenge is askin' ya to. The challenge says If "hash" has a key named "calories", then set the value of food equal to true. And literally thats it. You're tryin' way too much stuff. When it comes to challenges , just keep it simple and precise. Here is the code to clear away your confusions and doubts:

hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
food = true if hash.has_key? "calories"

And also has_keys? throws a boolean. In Ruby, any method that ends with "?"(question mark) , it throws a boolean. has_key? has an alias method called keys?, both methods throw similar results. Its just matter of style of coding. I like 'em expressive so i'd stick with has_key?. I hope it helped. (:

~ Ari