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

Challenge Task 2of2 Bug or code problem

What is wrong with my solution here for this task? If I remove the solution from task 1 or not, the line for adding an array with a value of item identifier does not work for me

grocery_list = [grocery_item.values_at('item')]

Thanks

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

2 Answers

There no need for square brackets

grocery_item = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }

if grocery_item.has_value?('Bread') == true
  grocery_item.store('food', true)
end

grocery_list = grocery_item.values_at('item')

The variable 'grocery_list' already knows it is going to be an Array, because you have used the 'values_at' method that returns an Array.

Hi, John, FYI, grocery_item.has_value?('Bread') returns a boolean value, thus there's no need to compare it against true. You could simply write it as

if grocery_item.has_value?('Bread')
  grocery_item.store('food', true)
end

William Li

Yep, you're right. I was whizzing through and didn't even consider that. Although you would find that my code would still pass.

Was just pointing out that the reason his code wasn't passing was due to the square brackets

Thanks for the answers. I see the square brackets,