Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!