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 Build a Grocery List Program Method Returns with Hashes and Arrays

Oğulcan Girginc
Oğulcan Girginc
24,848 Points

Modify 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'.

Alan Matthews
Alan Matthews
10,161 Points

The above code works when I try it out in my console, could you post the error you are getting?

2 Answers

Maximiliane Quel
PLUS
Maximiliane Quel
Courses Plus Student 55,489 Points

Hi 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
Oğulcan Girginc
24,848 Points

Thanks for the stating differences! :)

Alan Matthews
Alan Matthews
10,161 Points

The 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!