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 trialKetan Parikh
2,222 PointsChallenge 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
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
John Steer-Fowler
Courses Plus Student 11,734 PointsThere 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.
Ketan Parikh
2,222 PointsThanks for the answers. I see the square brackets,
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsHi, John, FYI,
grocery_item.has_value?('Bread')
returns a boolean value, thus there's no need to compare it againsttrue
. You could simply write it asJohn Steer-Fowler
Courses Plus Student 11,734 PointsJohn Steer-Fowler
Courses Plus Student 11,734 PointsWilliam 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