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 Values

What is the code used here to complete the challenge. Please explain why.

Please show me what code I have to use.

2 Answers

Seth Reece
Seth Reece
32,867 Points

You need to use an if statement to check if the hash has the key calories and if it does assign food = true

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

How were you able to answer this question without a code sample?

Seth Reece
Seth Reece
32,867 Points

There's a "View Challenge" button on the top right.

peyton caseria
peyton caseria
Courses Plus Student 12,281 Points

Seth Reece that only solves the first part. Unless I did something wrong.

peyton caseria
PLUS
peyton caseria
Courses Plus Student 12,281 Points
grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true }
grocery_item.has_value?(true)

this is the first part: I used http://ruby-doc.org/core-2.2.2/Hash.html#method-i-has_value-3F

scroll down to where it says has_value? Use Command + f on a mac to search the website for it. after you press command + f a search bar should come up on the right side of the screen. this is a trick question, the bread is already there, all you need to do is add ,"food" => true at the end of the hash, and then use the has_value? method to check if true is inside the grocery_item hash. that was already created for you.

the second part as a whole is:

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company", "food" => true }
grocery_list = grocery_item.values_at("item")
grocery_item.has_value?(true)

took me a while to figure out what they mean't! with a combination of that link and their video I figured it out, all you are doing is assigning .values_at to grocery_item then making grocery_list equal to grocery_item.values_at("item") I thought I had to make a separate array, which I did basically by doing the .value_at method, it just turned ("item") into its own array.