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

Chris Edwards
Chris Edwards
8,163 Points

Ruby Hash

This challenge #2.... https://teamtreehouse.com/library/ruby-collections/ruby-hashes/working-with-hash-values-2

Why does this answer not work?

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
if (grocery_item.has_value?("Bread"))
grocery_item["food"] = true
end

grocery_list = Array.new
grocery_list.push(grocery_item.values_at("item"))

6 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Chris Edwards! The reason the task fails is because the values_at method returns an array. The code challenge was looking for an array that looked like this:

["Bread"]

When an array is created using Array.new, it looks like this:

[]

When we use the values_at method, and push it on to this grocery_list variable, it looks like this:

[ ["Bread"] ]

It is now an array within an array which is why the code challenge was failing. I realize that this may be a bit confusing so I've updated the code challenge to look for any items that were added the way you did it. Your code should now be working. Great job!

I just felt like a superhero came and went right in front of us. Thanks Jason!

Wrap your code in '```' at the beginning and end. It'll make it easier to read. I apologise but at the moment I'm finding it hard to read it.

Hello! Chris, sorry for just getting back to you now. It's exam season for my Masters course, and I got distracted. :) Forgive me.

As for the answer, I just punched in character-for-character what your code has (in irb), and it returned => [["Bread"]] for me. I also did the task myself, and I got a success. Are you sure you entered the code exactly how you did here?

Chris Edwards
Chris Edwards
8,163 Points

I did but challenge part 2 fails.

It could possibly be because you have some 'whitespace' that you did not mean to add. Check it out, then get back to me. It's rather strange if it would work for me (using your code), but not work for you. Let me know.