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 trialOğulcan Girginc
24,848 PointsModify the "create_shopping_list" method to return a hash with the following keys and values:
def create_shopping_list
hash = {
"title" => "Grocery List",
"items" => []
}
end
Why the code above does work but the one below doesn't? Aren't they the same/similar?
def create_shopping_list
hash = {
title: "Grocery List",
items: []
}
end
Update:
The given error is :
Bummer! The create_shopping_list
method did not have a key called 'title' with the value of 'Grocery List'.
Oğulcan Girginc
24,848 PointsAdded the error! :)
2 Answers
Maximiliane Quel
Courses Plus Student 55,489 PointsHi Oğulcan,
the second code works on a console but not in the challenge, because the challenge specifically asks you to use the strings of 'title' and 'items' and not symbols with the same name as keys. Using symbols as keys is usually good practice. It is just not want the challenge was expecting :0)
Oğulcan Girginc
24,848 PointsThanks for the stating differences! :)
Alan Matthews
10,161 PointsThe create_shopping_list
methods should work, but I believe the error is due to the fact that hash
is scoped to those methods and cannot be accessed outside the methods. So it isn't an error per say, but it's probably not returning what you want. If you make a method like this:
def create_shopping_list
hash = {title: 'List', item: 'milk'}
puts hash[:title]
end
create_shopping_list
It should return List
. Hope this helps, I am not expert!
Oğulcan Girginc
24,848 PointsThanks! :)
Alan Matthews
10,161 PointsAlan Matthews
10,161 PointsThe above code works when I try it out in my console, could you post the error you are getting?